I’m currently trying to test whether it’s possible to make one object inherit from another object AFTER both objects have been created using literals. I tried to aim the constructors and prototypes at one another but it seems like no matter what, the only way Im going to pull this off is by building a new object using one of the pre-existing ones.. Let me know if I’m wrong. Here was my quick attempt to solve the problem.
Object.relate = function(parent, child){
function F(){};
F.prototype = parent;
child.constructor = F;
}
alpha = {a:1};
beta = {b:2};
Object.relate(alpha, beta);
In your code
betacan access properties defined onalphaby usingbeta.constructor.prototype.When you use
newon a function likethen
obj.__proto__is automatically set toF.prototype. When you have already existing objects and you are not creating the object usingnewyou must set the__proto__by yourself.So in your example you need to call
Resources:
The problem is that now every browser expose the
__proto__property. IE doesn’t support it so you cannot create a browser-compatible solution without actually creating a new object and copying the properties.