分別測試數組和對象
for in 適合訪問object, 訪問數組時會包含數組的其他屬性,例如數組名稱
for of適合訪問數組 , 訪問object報錯
var myArray=[1,2,4,5,6,7]
myArray.name="数组"
for (var index in myArray) {
console.log('log1',myArray[index]);
}
for (var value of myArray) {
console.log('log2',value);
}
var myObject={
a:1,
b:2,
c:3
}
for (var key in myObject) {
console.log('log3',key);
}
for (var key of myObject) {
console.log('log4',key);
}
結果如下