I would like (mainly for academic reasons) to set the accessor on the length array using Object.defineProperty() , so how can I notify about size changes.
I am aware of the observation of the ES6 object and watch.js, but I would like to try to do it in ES5 without additional libraries, if possible, even if it is only for V8 / Chrome.
Array example:
var demoArray = ['one', 'two']
Alas Chrome, out of the box, makes the length not customizable:
Object.getOwnPropertyDescriptor(demoArray, 'length') Object {value: 2, writable: true, enumerable: false, configurable: false}
And this does not work:
Object.defineProperty(demoArray, 'length', { set: function(){ console.log('length changed!')} })
Crash with 'TypeError: Cannot redefine property: length'
As you can see, configurable false - so the failure is clear. However , according to MDN, this should be possible .
How can I get defineProperty while working on an array length property? Should this work?