测试数组testArray

testArray[[1,2,3],[3,4,4,4],["string","yes"]]

(3) [Array(3), Array(4), Array(2)]

0: (3) [1, 2, 3]
1: (4) [3, 4, 4, 4]
2: (2) [“string”, “yes”]
length: 3
proto: Array(0)

for in 循环:


for (i in testArray){
console.log(i);
}

i是testArray的对象的属性值(properties of an object),所以输出:

0
1
2

for of 循环:


for (i of testArray){
console.log(i);
}

i是testArray一个对象,就是键值对,i也就是key,所以输出:

(3) [1, 2, 3]
(4) [3, 4, 4, 4]
(2) [“string”, “yes”]

for (i;i<len;i++){} 循环:


for (i=0;i<testArray.length;i++){
console.log(testArray[i]);
}

输出结果和for of是一样的:

(3) [1, 2, 3]
(4) [3, 4, 4, 4]
(2) [“string”, “yes”]




发表评论

电子邮件地址不会被公开。 必填项已用*标注