一句话总结:for.,.in是为遍历对象属性而构建的,遍历的是index,而for,,,of是为了遍历数组的,遍历的
是value

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
const arobj =
100:1,
d:2,
a:9,
1:3,
5:99,
b:8
}
Object.prototype.fun =()=>{}
arobj.name = "lallala"
const arr=[1,2,4,8,9,10]
Array.prototype.method ()=>{}
arr.name ="bababa";
for (const item in arobj){
console.log(arobj[item])//3 99 1 2 9 8 lallala ()=>{
}
for (const item in arr){
console.log(item)//0 1 2 3 4 5 name method fun
}
for (const item of arobj){
console.log(item)//Uncaught TypeError:arobj is not iterable
}
for (const item of arr){
console.log(item)//1 2 4 8 9 10
}

一、for..in
1.for..in适合遍历对象,遍历数组的时候会出现奇奇怪怪的问题
2.for.in遍历会遍历所有的可枚举属性,包括原型链上的,可以使用hasOwnProperty来过滤
3.for..in中index索引为字符串型数字,不能直接进行几何运算
二、for..of
1.适合比遍历所有拥有迭代器的对象的集合