Does the JScript script describe changes to instances?

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 ):

//works as expected, overloading complete       
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()); //42
var test2 = new Child();
console.log(test2.answer()); //0

//does not work as expected
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()); //42
test4 = new Child2();
test4.answer = function() { return 0; };
console.log(test4.answer()); //42

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.

, ? , ?

+4
3

, .

.

. : . "use strict" mode,

Error { message: "Invalid assignment in strict mode", … }

test4.answer = function() { return 0; };

(test2), (test4)

. , , :

  • , ,
  • Object.defineProperty ,

:

Object.defineProperty(test4, "answer", {
    value: function() { return 42; }
});
+3

, ( ), (), , , (). . EcmaScript-262 9.1.9 1.c.i

4. If ownDesc is undefined, then
  a. Let parent be O.[[GetPrototypeOf]]().
  b. ReturnIfAbrupt(parent).
  c. If parent is not null, then
      i. Return parent.[[Set]](P, V, Receiver).

, , .

var proto = Object.defineProperties({}, {
  foo: {
    value: "a",
    writable: false,  // read-only
    configurable: true  // explained later
  }
});

var instance = Object.create(proto);
Object.defineProperty(instance, "foo", {writable: true});
instance.foo // "b"
+2

: https://jsfiddle.net/s7wdmqdv/1/

var Parent = function() {};
Object.defineProperty(Parent.prototype,"type", {
    value: function() { return 'Parent'; }
});
var oParent = new Parent();
console.log('parent', oParent.type()); // Parent


var Child1 = function() {};
Child1.prototype = Object.create(Parent.prototype, {
    type: {
        value: function() { return 'Child1'; }
    }
});
var oChild1 = new Child1();
console.log('child1', oChild1.type()); // Child1


var Child2 = function() {};
Child2.prototype = Object.create(Parent.prototype);
Object.defineProperty(Child2.prototype, 'type', {
    value: function() { return 'Child2'; }
});
var oChild2 = new Child2();
console.log('child2', oChild2.type()); // Child2


var Child3 = function() {};
Child3.prototype = Object.create(Parent.prototype);
var oChild3 = new Child3();
oChild3.type = function() { return 'Child3'; };
console.log('child3', oChild3.type()); // Parent


var Child4 = function() {};
Child4.prototype = Object.create(Parent.prototype);
Child4.prototype.type = function() { return 'Child4'; };
var oChild4 = new Child4();
console.log('child4', oChild4.type()); // Parent


Object.defineProperty(Parent.prototype,"type", {
    value: function() { return 'Parent2'; }
});
var oParent2 = new Parent();
console.log('parent2',oParent2.type());

Object.create(...) , .

- child.answer = 10 Child.prototype.answer.writable. , Child.prototype.constructor.prototype.answer.writable, .

, Object.defineProperty(Child.prototype, ...), . , Child.prototype. , .

0

Source: https://habr.com/ru/post/1627144/