数组
const array = new Array()
const array = []
如此可以创建数组
类数组和可迭代对象可以用Array.from来获得一个真正的数组
类数组
类数组和数组类似,可以用for of 循环可以通过下标访问,但是没有数组的方法。
document.querySelectorAll() 返回的就是一个类数组。
如下图所示:就是一个类数组
可迭代对象
let range = {
from: 1,
to: 5,
[Symbol.iterator]() {
this.current = this.from;
return this;
},
next() {
if (this.current <= this.to) {
return { done: false, value: this.current++ };
} else {
return { done: true };
}
}
};
for (let num of range) {
alert(num); // 1, 然后是 2, 3, 4, 5
}
上面的代码实现的就是一个可迭代对象
可迭代对象就是一个内部自己实现了for of循环的对象