When defining an array as a property of an ES5-style object, I want to make it so the property's value cannot be changed.
'use strict';
var global = Object.create(Object.prototype, {
names: {
value: ['Barney', 'Trogdor'],
writable: false
}
});
global.names.push('Jackson'); // I expected a read-only error here
console.log(global.names[2]); // >> Jackson
global.names = ['Ooga', 'Booga']; // >> TypeError: "names" is read-only
It seems that I have only protected against property assignment.
Is there any way to protect against stuff like Array.push()
that modifies my "unwritable" array?