I'm working on a code similar to the following snippet:
let arr = [];
let arr_handlers = {
set: function(target, property, value, receiver) {
console.log('setting ' + property + ' for ' + target + ' with value ' + value);
target[property] = value;
return true;
}
};
let arr_proxy = new Proxy(arr, arr_handlers);
arr_proxy.push(1);
// setting 0 for with value 1
// setting length for 1 with value 1
arr_proxy[1] = 5;
// setting 1 for 1 with value 5
console.log(arr.length);
// 2
The problem is that proxy behaves differently when I use arr_proxy.push()
and when I set value through index:
Why proxy setter isn't invoked to set length when array value is set by index?