📢 数组中forin迭代到的属性类型为字符串类型
forof
let a = [4, 5, 6, 7];
for (let v of a) {
console.log(v, typeof v)
}
// 4 number
// 5 number
// 6 number
// 7 number
forin
let a = [4, 5, 6, 7];
for (let idx in a) {
console.log(idx, typeof idx)
}
// 0 string
// 1 string
// 2 string
// 3 string
typeof 'e' // 'string'
Set元素可用for-of遍历得到
Set + for of
let a = new Set();
a.add(1);
a.add(2);
for(let key of a) console.log(key, typeof key)
// 1 number
// 2 number
for(let key in a) console.log(key, typeof key)
// none