Stuck at adding methods after inheritance

I have been trying to solve it in some other way but it’s not working.
This is the solution provided by freeCodeCamp


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(){
// bark:()=>{
//   console.log('Woof!');
// }
console.log('Woof!');
}
// Only change code above this line

let beagle = new Dog();
beagle.eat();
beagle.bark();

This is the way i was trying to solve it.

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:()=>{
   console.log('Woof!');
 }
}



// Only change code above this line

let beagle = new Dog();
beagle.eat();
beagle.bark();

  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36

Challenge: Add Methods After Inheritance

Link to the challenge:

Hello there, the problem is that you cannot use arrow functions inside a method. See here: Arrow function expressions - JavaScript | MDN. Hope this helps.

Thank you for the info! :grinning:

1 Like