数组的问题
在 ES6 之前,JS 的数组对象拥有特定的行为方式,无法被开发者在自定义对象中进行模拟。当你给数组元素赋值时,数组的 length 属性会受到影响,同时你也可以通过修改 length 属性来变更数组的元素。例如:
let colors = ["red", "green", "blue"];
console.log(colors.length); // 3
colors[3] = "black";
console.log(colors.length); // 4
console.log(colors[3]); // "black"
colors.length = 2;
console.log(colors.length); // 2
console.log(colors[3]); // undefined
console.log(colors[2]); // undefined
console.log(colors[1]); // "green"
colors 开始时有三个元素。把 "black" 赋值给 colors[3] 会自动将 length 增加到 4;而此后设置 length 为 2 则会移除数组的最末两个元素,从而只保留起始处的两个元素。在 ES5 中开发者无法模拟实现这种行为,但代理的出现改变了这种情况。
这种不规范行为就是 ES6 将数组认定为奇异对象的原因。 |