Reputation: 3082
i've been experimenting with javascript's prototypal inheritance and have come across something that perhaps can be explained.
function dinner(food,drink){
this.food=food;
this.drink=drink;
}
dinner.prototype.desert=function(){
var x = this.food;
return x.split(' ')[0]+' Ice Cream Float';
}
function superSupper(steak){
this.steak=steak;
}
superSupper.prototype= new dinner();
superSupper.prototype.constructor=superSupper;
var x = new superSupper('corn','beet juice')
x.grub='beef';
x.clams = 'nope';
in the above code i'm making a new construtor "superSupper" and making it inherit from dinner. when this is viewed in a console.log i see this:
superSupper
clams: "nope"
grub: "beef"
steak: "corn"
__proto__: dinner
constructor: function superSupper(steak){
drink: undefined
food: undefined
__proto__: dinner
how do i access the drink and food properties that i have now inherited from dinner?
p.s. trying this: "x.food='some string'" only creates a new property in the superSupper instance called food, but does not assign a value to the inherited food property.
Upvotes: 1
Views: 111
Reputation: 3427
You have to modify your superSupper
a bit:
function superSupper(steak){
// calling superclass constructor
dinner.apply(this, arguments);
this.steak=steak;
}
Upvotes: 5