I have some object, say son , that I would like to inherit from another father object.
Of course, I can create a constructor function for my father, for example
Father = function() { this.firstProperty = someValue; this.secondProperty = someOtherValue; }
And then use
var son = new Father(); son.thirdProperty = yetAnotherValue;
but thatβs not quite what I want. Since son will have many properties, it would be more readable if the son were declared as an object literal. But then I do not know how to install it protoype.
Doing something like
var father = { firstProperty: someValue; secondProperty: someOtherValue; }; var son = { thirdProperty: yetAnotherValue }; son.constructor.prototype = father;
will not work because the prototype chain seems hidden and does not care about changing the constructor. prototype.
I think I can use the __proto__ property in Firefox, for example
var father = { firstProperty: someValue; secondProperty: someOtherValue; }; var son = { thirdProperty: yetAnotherValue __proto__: father }; son.constructor.prototype = father;
but, as I understand it, this is not a standard feature of the language, and itβs better not to use it directly.
Is there a way to specify a prototype for an object literal?
javascript oop prototype-programming
Andrea
source share