When working with JavaScript ES6 Proxies, the set property trap for array.length does not fire when assigning array indexes directly.
For example:
const proxy = new Proxy([], {
set: function(obj, name, value) {
console.log(`set: ${name}`);
obj[name] = value;
return true;
}
});
proxy.push(0);
proxy[1] = 1;
Chrome 51 and Firefox 47 outputs:
set: 0 set: length set: 1
While I would expect:
set: 0 set: length set: 1 set: length
Is this per spec? I couldn't find any information on this.
There is no need to explicitly set the length
property when a value is assigned to an index. The reason why it's set with push
is indeed defined in the specification:
Repeat, while items is not empty
a. Remove the first element from items and let E be the value of the element.
b. Let setStatus be Set(O, ToString(len), E, true).
c. ReturnIfAbrupt(setStatus).
d. Let len be len+1.
- Let setStatus be Set(O, "length", len, true).
Basically: If an error happens then set the correct length in case the array already has been expanded.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With