Adding Methods After Inheritance

Tell us what’s happening:

why does my code not work?

Your code so far


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,
bark:function(){console.log("Woof!");}
};

// Only change code above this line

let beagle = new Dog();
console.log(beagle.bark() );

Your browser information:

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

Challenge: Add Methods After Inheritance

Link to the challenge:

Here you’re re-assigning prototype instead of adding methods to it

I frankly have a beef with this challenge as, putting aside that no one really does inheritance based OOP, certainly no-one uses this pre-2015 syntax. It was ok the first couple years as transition, but any transition has to come to the end. FCC needs some cleaning to be done here.

This is how it’s normally done:

class Animal {
  eat() {
    console.log("nom nom nom");
  }
}

class Dog extends Animal {
  bark() {
    console.log("Woof!");
  }
}

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

Clean, readable and easy to grasp. The learning process should go from easiest to hardest concept and not from oldest to newest!

2 Likes