Let's 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 which its prototype is 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 fine! No problem. But now at runtime I want to change the prototype of u
to something else. If u
was created with a constructor like this:
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 totally 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 whatever I added to the prototype of U
would automatically be added to every object that is created after those changes. But how do I get the same effect with Object literals?
Please provide the simplest solution which has a clean and short syntax. I just wanna know if it's possible to do this manually. I don't want to use the non-standard __proto__
keyword either.
I've searched Stackoverflow and this is not really a duplicate of the following questions:
Because I want to change the prototype after creation in a standard way (if possible). Something like Object.setPrototype()
would be perfect, but that function doesn't exist! Is there any other way to simply set the prototype of an object that is created using object literal initialization?