Answers (please read them below, their respective authors provided valuable information):
- "writeable: false" prevents the assignment of a new value , but
Object.defineProperty is not an assignment operation and therefore ignores the value of "writable"
- property attributes are inherited, so the property will remain inaccessible for writing on each subclass / instance, until one subclass (or instance of the subclass) changes the value of "written" back to true for itself
Question
MDN documentation related to properties for a property descriptor writable:
written true if and only if the value associated with the property can be changed using the assignment operator. The default is false.
The official 6th edition of ECMA-262 more or less claims the same thing. The meaning is clear, but as far as I know, it was limited to the original property (i.e., Property on this particular object)
However, please consider the following example ( JSFiddle ):
var Parent = function() {};
Object.defineProperty(Parent.prototype, "answer", {
value: function() { return 42; }
});
var Child = function() {};
Child.prototype = Object.create(Parent.prototype, {
answer: {
value: function() { return 0; }
}
});
var test1 = new Parent();
console.log(test1.answer());
var test2 = new Child();
console.log(test2.answer());
var Parent2 = function() {};
Object.defineProperty(Parent2.prototype, "answer", {
value: function() { return 42; }
});
var Child2 = function() {};
Child2.prototype = Object.create(Parent2.prototype);
test3 = new Parent2();
console.log(test3.answer());
test4 = new Child2();
test4.answer = function() { return 0; };
console.log(test4.answer());
Following this example, we see that although the property is not writable, it can be overloaded with the prototype subclass (test2), as you would expect.
However, when you try to overload the method in an instance of a subclass (test4), it fails. I would expect it to work just like test2. The same thing happens when you try to overload the property of the parent instance.
NodeJS, JSFiddle, , , TypeError, readonly.
, ? , ?