Reset an Inherited Constructor Property

In this code bellow:

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

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

// Only change code below this line



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

console.log(Dog.prototype.isPrototypeOf(duck))

why the log is false if such as Dog and Bird were instanceOf Animal, why only in Bird.prototype the log returns true ?

There are two prototype chains stemming from the parent Animal prototype.

Animal —> Bird —> duck
Animal —> Dog —> beagle

When the isPrototypeof() method is called on Dog , it searches its prototype chain and duck is nowhere to be found. That is not true when Dog is replaced by Bird because Bird and duck are a part of the same chain.

Think of it as:
Same Parent —> Child —> Grandchild
Same Parent —> Different Child —> Different Grandchild

They share some blood, but there are differences between the cousins (grandchildren).

EDIT:
It is also worth mentioning that isPrototypeOf() works on objects, not prototypes themselves.
Animal.prototype.isPrototypeOf(Bird) will return false.
Animal.prototype.isPrototypeOf(duck) will return true.

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