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 );
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 );
OR
function U () { this.name = "Jack"; } var u = new U; console.log( u.name );
But when using constructors, I can completely change the prototype :
function U () {}
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?
source share