Why when using Object.create to create the property __proto__?

6

I just asked this question How to clone an object in javascript?

And I had another question. When I create an object from another with Object.create , the __proto__ attribute containing the contents of the object passed by the create argument is added to the new object.

So:

function writeObject(obj)
{
    document.write(JSON.stringify(obj));
}

a = {nome: 'wallace'}

b = Object.create(a);

writeObject(a);
writeObject(b);
writeObject(b.__proto__);

What does this __proto__ mean?

    
asked by Wallace Maxters 26.11.2015 в 18:19

2 answers

6

__proto__ is the prototype of the object (as opposed to the .prototype of a function, which is not a prototype of it, but of the object it creates when invoked as a constructor).

Only __proto__ was a nonstandard property until recently (even though it is available in all implementations I know of), so it should not be used. Nothing guarantees that it exists in its implementation (think of the future!). The fact is that in JavaScript, up to ES5, there is no way to change the prototype of an existing object without using __proto__ . That's what it's for, and it should be used very carefully for performance issues.

In ES6 (ECMA-2015 onwards), __proto__ was included in the specification for compatibility purposes, but has already been marked as deprecated, in favor of Object.setPrototypeOf and% with%. But even with standardization, changing the on-the-fly prototype continues to be contraindicated for performance issues.

    
26.11.2015 / 19:11
3

The first parameter of this Object.create method is exactly the object that should be the prototype for creating the new object. Hence this parameter is called proto (see MDN page ) and accessible via __proto__ , ie it is the object reference passed as argument to Object.create to create the new object.

var original = {};
var novo = Object.create(original);

console.log(original == novo); // false
console.log(original == novo.__proto__); // true
    
26.11.2015 в 19:07