Reputation: 161
I'd like to call a function from Mammal, to use with Cat. I thought I understood it, but every time I try to use it, I get really confused.
function Mammal(legs,sound, commonName) {
this.legs = legs;
this.sound = sound;
this.commonName = commonName;
this.talk = function() {
console.log(this.sound);
}
}
const wolf = new Mammal(4, 'GRRRRRR', 'Wolf');
const dog = new Mammal(4, 'WOOF', 'Dog');
console.log(wolf)
console.log(dog.talk())
const cat = function(legs, sound, commonName) {
this.legs = legs;
this.sound = sound;
this.commonName = commonName;
Mammal.call(this, talk)
}
const lion = new cat(4, 'RAWR', 'Lion');
I want to use talk, with the context of lion.
Upvotes: 0
Views: 63
Reputation: 181
You were suuuper close. You just need to add the parameters in the Mammal.call() function.
function Mammal(legs,sound, commonName) {
this.legs = legs;
this.sound = sound;
this.commonName = commonName;
this.talk = function() {
return this.sound;
}
}
const wolf = new Mammal(4, 'GRRRRRR', 'Wolf');
const dog = new Mammal(4, 'WOOF', 'Dog');
const cat = function(legs, sound, commonName) {
this.legs = legs;
this.sound = sound;
this.commonName = commonName;
Mammal.call(this, legs, sound, commonName);
}
const lion = new cat(4, 'RAWR', 'Lion');
console.log(lion.talk())
I changed Mammal.call(this, talk) to Mammal.call(this, legs, sound, commonName).
I hope this is what you were asking for! Let me know if it isn't.
Edit: I also just noticed that I replaced the console.log() inside of the "talk" function to "return this.sound" and then the very last line I'm doing "console.log(lion.talk())"
Upvotes: 2