`for in` 遍历的是一个对象的索引, 而`for of `遍历的时候数组元的值,
`for in` 总是得到对象的 `key` 或 数组和字符串 的 下标(索引)
`for of` 总是得到的数组,字符串的值,(有迭代器的对象)
示例:
let arr = [1,2,3]
Array.prototype.a = 123
for (let index in arr) {
let res = arr[index]
console.log(res)
}
//1 2 3 123
for(let index in arr) {
if(arr.hasOwnProperty(index)){
let res = arr[index]
console.log(res)
}
}
// 1 2 3
总结:
for of适用遍历数/数组对象/字符串/map/set等拥有迭代器对象(iterator)的集合,但是不能遍历对象,
因为没有迭代器对象,但如果想遍历对象的属性,你可以用for in循环(这也是它的本职工作)或用内建的Object.keys()方法