Hello JS guys,
I am new in JS and trying to solve a problem named [Add Methods After Inheritance] in OOP js. My solution is given below:
function Animal() { }
Animal.prototype.eat = function() { console.log("nom nom nom"); };
function Dog() { }
// Only change code below this line
Dog.prototype = Object.create(Animal.prototype);
Dog.prototype.constructor = Dog;
Dog.prototype.bark = function(){console.log("Woof!");};
// Only change code above this line
let beagle = new Dog();
beagle.eat(); //gives nom nom nom
beagle.bark(); //gives Woof!
console.log(beagle.eat()); //gives "nom nom nom" and "undefined"
console.log(beagle.bark()); //gives "Woof!" and "undefined"
If beagle.bark();
→ Woof!
but if console.log(beagle.bark()))
→ Woof! undefined
.
My qs is why that undefined
in terms of console.log()
? I don’t get it.