I tried the following code in chrome's console
var a = new Array(1,2,3,4);
a.length
This shows length as 4 as expected. Now I tried setting length property as writable: false
Object.defineProperty(a, "length", {writable: false});
a[4] = 5;
a.length
This results in 5 even though the property is set to writable:false. How did that happen? Shouldn't it have remained the same as it is set to read-only(writable:false)?
Object.defineProperty(a, "length", {writable: false});
only affect the way you assign the value directly to the .length
property.
var a = [1,2,3,4];
Object.defineProperty(a, "length", {writable: false});
a.length = 0;
console.log(a.length); // still be 4
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