Add Methods After Inheritance

Add Methods After Inheritance
0

#1

I guess, this solution should be right… ? But it is not being accepted.
Any clue?

Dog.prototype = Object.create(Animal.prototype);
Dog.prototype.constructor = Dog;
Dog.prototype = {
    bark: function(){
        console.log("Grrrr!");
    }
};

#2

Can you add the link to the challenge here?


#3

Sure, sorry!

https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/object-oriented-programming/add-methods-after-inheritance


#4

Look at the example in the challenge instructions where the fly method was added. That is the correct syntax. The 3rd line of your code redefines the Dog prototype, so it is no longer inheriting from Animal, so it no longer has access to the eat method. Another issue with your code is that the bark function does not console.log the correct string.


#5

@RandellDawson, Yes I brought that to work following the syntax in the example.
I was just wondering if the syntax shown in the link below would also work:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/object-oriented-programming/change-the-prototype-to-a-new-object


#6

Yes, you could have, but you would have been missing the eat prototype method which was inherited from Animal.


#7

Completely understood now. Thanks again!


split this topic #8

3 posts were split to a new topic: JavaScript Add Methods After Inheritance challenge


#9

I think below code will pass your all the test cases

Dog.prototype = Object.create(Animal.prototype);//Dog inherit eat property from animal
Dog.prototype.constructor = Dog;
Dog.prototype.bark = function(){
console.log("Woof");
}//Bark property 

#10

function Animal() { }

Animal.prototype.eat = function() { console.log(“nom nom nom”); };

function Dog() { }

// Add your code below this line

Dog.prototype = Object.create(Animal.prototype);

Dog.prototype.constructor = Dog;

Dog.prototype.bark = function(){console.log(“Woof!”);

};

Dog.prototype.eat = function(){console.log();

};

// Add your code above this line

let beagle = new Dog();

beagle.eat(); // Should print “nom nom nom”

beagle.bark(); // Should print “Woof!”