I Don't Understand Object Oriented Programming - Understand the Prototype Chain

Tell us what’s happening:
Hello everyone,

I pass this section because I read the instruction. but I don’t quite understand why

Dog.prototype.isPrototypeOf(beagle);

return true

and this one:

beagle.isPrototypeOf(Dog.prototype);

return false

Could someone explain it in a simple way please

Your code so far

function Dog(name) {
  this.name = name;
}

let beagle = new Dog("Snoopy");

Dog.prototype.isPrototypeOf(beagle);  // yields true

// Fix the code below so that it evaluates to true
beagle.isPrototypeOf(Dog.prototype);

Your browser information:

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

Challenge: Object Oriented Programming - Understand the Prototype Chain

Link to the challenge:

What if I say:

every beagle is a dog? that is kinda true, right? At least in the context of this challenge, not going into english language details here.

So if I rephrase it in more ‘javascript’ fashion’, I can say that dog is a prototype of beagle.

And if I translate the question ‘is a dog a prototype of beagle?’ into javascript, I should write exactly this:

Dog.prototype.isPrototypeOf(beagle);

we defined beagle in the upper code as an instance of Dog. So, the above expression evaluates to true.

here is your code a bit modified, with some examples:

function Dog(name) {
  this.name = name;
}

function Car(name) {
  this.name = name;
}

let beagle = new Dog("Snoopy");
let ford = new Car("good old ford")

console.log(Dog.prototype.isPrototypeOf(beagle));//true

console.log(Car.prototype.isPrototypeOf(beagle))//false

console.log(Dog.prototype.isPrototypeOf(ford))//false

console.log(Car.prototype.isPrototypeOf(ford))//true

I understand the example you gave, since it’s obvious difference.

I was confused with why Dog prototype isPrototypeOf beagle but not the otherway around. but I think I got it now. because Dog is supertype of beagle but not the vice versa. correct me if im wrong

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