Changing the prototype of an object that was created with literal initialization

Say I want to use ONLY object literals (not constructors). I have an object like this:

var o = { name : "Jack" } 

If I want to create another object, its prototype o , I use this syntax:

 var u = Object.create( o ); console.log( u.name );//prints Jack u.name = "Jill"; console.log( u.name );//prints Jill 

Works great! No problems. But now at runtime, I want to change the u prototype to something else. If u was created using such a constructor:

 function U () {} U.prototype.name = "Jack"; var u = new U; console.log( u.name );//prints Jack 

OR

 function U () { this.name = "Jack"; } var u = new U; console.log( u.name );//prints Jack 

But when using constructors, I can completely change the prototype :

 function U () {} //totally changed the prototype object to another object U.prototype = { dad : "Adam" } var u = new U; console.log( u.dad );//prints Adam 

Then, everything that I added to the prototype u is automatically added to each object created after these changes. But how do I get the same effect from Object literals?

Please provide a simple solution with clean and short syntax. I just want to know if this can be done manually. I do not want to use non-standard __proto__ .

I searched Stackoverflow and this is not a duplicate of the following questions:

Because I want to change the prototype after creating it in the standard way (if possible). Something like Object.setPrototype() would be ideal, but this function does not exist! Is there any other way to simply install a prototype of an object that is created by initializing the object literal?

+4
source share
1 answer

With the current specification, you cannot change the prototype of an object as soon as it is created (as in the case, replace it and replace in another). (But see below, everything can change.) You can only change the prototype of an object. But that may be all you need when looking at your question.

To understand this difference:

 var p1 = { foo1: function() { console.log("foo1"); } }; var p2 = { foo2: function() { console.log("foo1"); } }; var o = Object.create(p1); o.foo1(); // logs "foo1" o.foo2(); // ReferenceError, there is no foo2 // You cannot now *change* o prototype to p2. // You can modify p1: p1.bar1 = function() { console.log("bar1"); }; // ...and those modifications show up on any objects using p1 // as their prototype: o.bar1(); // logs "bar1" // ...but you can't swap p1 out entirely and replace it with p2. 

Returning to your question:

If u was created with a constructor like this ... Then everything I added to the U prototype will be automatically added to every object created after these changes. But how do I get the same effect from Object literals?

By changing the object that you passed to Object.create as a prototype, as described above. Note that adding bar1 to p1 made it available in o , although o was created before it was added. As in the case of design functions, the relationship between the prototype is preserved, o does not receive a snapshot of p1 from the moment of its creation, it receives a strong link to it.


In ES.next, it will most likely be installed to install the operator prototype "( <| ), which will allow this, but there is currently no standard mechanism for this. Some engines implement a pseudo-property called __proto__ , which now provides this function, but not standardized.

+6
source

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


All Articles