遍历 Object
for - of
var obj = {
a: 1,
b: [],
c: function () {}
};
for (var key of obj) {
console.log(key);
}
// 出错:
// Uncaught TypeError: obj is not iterable
for-in
var obj = {
a: 1,
b:[],
c:function () { }
};
for(var key in obj){
console.log(key)
}
// 结果是:
// a
// b
// c
遍历数组
for-in
var arr = [3,5,7];
for (var i in arr) {
console.log(i)
}
结果是 0 1 2
for-of
var arr = [3, 5, 7];
for (var i of arr) {
console.log(i);
}
// 结果是:
// 3
// 5
// 7
总结:
1. for-in 无法遍历