Home>

There are many places on the Internet, for example, it is argued that when a property is written to an object, even if there is no such property in the object yet, but is in the prototype, the property will simply be created in the object without the participation of the prototype. That is, it is assumed that if there is

const animal= {
  name: 'somename',
};
const cat= {
  __proto__: animal,
};

and I will docat.name= 'Vaska', then name from animal will not be used at all. However, if you set the descriptor to the name property of animalwritable= falsethen cat will get an error while writing the property.

'use strict';
const animal= {
  name: 'somename',
};
const cat= {
  __proto__: animal,
};
Object.defineProperty (animal, 'name', {writable: false});
cat.name= 'Vaska';
console.log (cat); 

Explain, please, what sequence of actions occurs with such a recording.

And in chrome under linups there is no assignment error (it seems that there should not be any in the spec) ... although cat does not create its own property (i.e., the same thing happens as when trying to change a non-writable property -such an attempt just ignored).

yar852021-10-14 03:33:11

yes, did not specify, error in strict mode. In normal, the action is simply ignored.

Роман2021-10-14 03:33:11

The simplest assumption is that the interpreter first calculates both operands= (on the left side of the expression, looking for a reference to a property /contextual record ... and since the object does not have its own property, it takes a ref on the prototype), and only then tries use this ref to write the value, i.e. the assignment operation itself. If this really happens, then it turns out that the only way to create your own property in such cases is the same static defineProperty method (it doesn’t seem to follow the prototype chain, but I don’t remember exactly, I won’t lie).

yar852021-10-14 03:33:11

everything is a little more complicated, and the assertion when writing a property to an object, even if there is no such property in the object yet, but is in the prototype, the property will simply be created in the object without the participation of the prototype -it works in this form only for data-properties whose writable is true ...

Grundy2021-10-14 03:33:11

Interestingly, this behavior can change depending on the type of object to which the property is assigned.

Grundy2021-10-14 03:33:11