//對於普通的對象,for...of結構不能直接使用,會報錯,必須部署了 Iterator 接口後才能使用,for-of用於數組對象。
//但是,這樣情況下,for...in循環依然可以用來遍歷鍵名。
let es6 = {
edition: 6,
committee: "TC39",
standard: "ECMA-262"
};
for (let e in es6) {
console.log(e);
}
// edition
// committee
// standard
for (let e of es6) { // TypeError: es6[Symbol.iterator] is not a function//ES6不是迭代的,不能直接使用for of 必須轉成數組
console.log(e);
}
//上面代碼表示,對於普通的對象,for...in循環可以遍歷鍵名,for...of循環會報錯。
//一種解決方法是,使用Object.keys方法將對象的鍵名生成一個數組,然後遍歷這個數組。
console.log(Object.keys(es6));//["edition", "committee", "standard"]
for(let e of Object.keys(es6)){
console.log(e);
}
//edition
//committee
//standard