The Array Problem
The JavaScript array object behaves in ways that developers couldn’t mimic in their own objects before ECMASCript 6. An array’s length
property is affected when you assign values to specific array items, and you can modify array items by modifying the length
property. For example:
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"
The colors
array starts with three items. Assigning "black"
to colors[3]
automatically increments the length
property to 4
. Setting the length
property to 2
removes the last two items in the array, leaving only the first two items. Nothing in ECMAScript 5 allows developers to achieve this behavior, but proxies change that.
I> This nonstandard behavior is why arrays are considered exotic objects in ECMAScript 6.