for…in和for…of都是用来遍历属性的
for…in循环:只能获得对象的键名,不能获得键值
for…of循环:允许遍历获得键值 (es6的新方法)
// 1.遍历对象的区别
function Foo() {
this.name = '努力';
this.age = 18
}
Foo.prototype.getName = function () {
return this.name
}
var foo = new Foo();
for(var inkey in foo) {
if(foo.hasOwnProperty(inkey)){
console.log(inkey)
}
}
// for...of用来遍历具有interator接口的数据结构
// 在此处使用会报错