I have some object, say son
, which I'd like to inherit from another object father
.
Of course I can make a constructor function for father, like
Father = function() {
this.firstProperty = someValue;
this.secondProperty = someOtherValue;
}
and then use
var son = new Father();
son.thirdProperty = yetAnotherValue;
but this is not exactly what I want. Since son
is going to have many properties, it would be more readable to have son declared as an object literal. But then I don't know how to set its protoype.
Doing something like
var father = {
firstProperty: someValue;
secondProperty: someOtherValue;
};
var son = {
thirdProperty: yetAnotherValue
};
son.constructor.prototype = father;
will not work, as the prototype chain seems to be hidden and not care about the change of constructor.prototype.
I think I can use the __proto__
property in Firefox, like
var father = {
firstProperty: someValue;
secondProperty: someOtherValue;
};
var son = {
thirdProperty: yetAnotherValue
__proto__: father
};
son.constructor.prototype = father;
but, as far as I understand, this is not a standard feature of the language and it is better not to use it directly.
Is there a way to specify the prototype for an object literal?