Why this code is not working of JavaScript OOP? JavaScipt experts please explain!

Tell us what’s happening:
I just cannot understand why the following code is not working even if the1st and 2nd code block are similar?

1st Code :

function Animal() { }
function Bird() { }
function Dog() { }

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

// Problem with code below this line
Bird.prototype = {
  constructor : Bird

Dog.prototype = {
  constructor : Dog
// Problem with code above this line

let duck = new Bird();
let beagle = new Dog();

The above code shows two errors below :

1. Bird.prototype should be an instance of Animal.
2. Dog.prototype should be an instance of Animal.

Even if the the above code (1st code) is similar to below code (2nd code).

Your code so far (2nd code) :

Bird.prototype.constructor = Bird;
Dog.prototype.constructor = Dog;

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:73.0) Gecko/20100101 Firefox/73.0 .

Challenge: Reset an Inherited Constructor Property

Link to the challenge:

Thanks in advance! :pray:

The reason this is not working is because you are completely reassigning the Dog and Bird prototypes to a new object.
If you replace this with the second piece of code you have, the tests should pass.

1 Like

Thank you for your response. It means a lot! :pray:

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.