Not understanding output of the .isPrototypeOf() method

I’m just curious why do the codes returns false and TypeError respectively. What makes them different?

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

let beagle = new Dog("Snoopy");

console.log(Object.prototype.isPrototypeOf(beagle.prototype))   //false
console.log(beagle.prototype.isPrototypeOf(beagle))       // TypeError
  **Your browser information:**

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

Challenge: Understand Where an Object’s Prototype Comes From

Link to the challenge:

beagle doesn’t have a prototype property. Dog has a prototype property because it is a constructor function, but beagle is an object, not a function, so it doesn’t have the prototype property.

Your first console log should be:
console.log(Object.prototype.isPrototypeOf(beagle))

Now you are checking if the object beagle exists in Object's prototype chain.

Your second console log is an error because there is no prototype property for beagle.

I’m aware of the above. I’m just wondering since beagle.prototype is not a valid object, why don’t both console.log return an Error. Why the first one return false instead?

Oh and another question pops up in my head now that you’ve mentioned this:

Are we checking if beagle exists in Object’s prototype chain or the other way round, if Object exists in beagle’s prototype chain ?

beagle.prototype would be a property on the object beagle. Since the prototype property doesn’t exist then beagle.prototype returns undefined which give us:

Object.prototype.isPrototypeOf(undefined)

which returns false. Replace undefined with any other primitive type (a number, string, boolean, null) and it returns false as well. The job of the function isPrototypeOf is not to check if the value passed in is actually an object, but rather just to check if its prototype chain leads back to the calling object. So if the value passed in is not an object then it is an automatic return of false since it doesn’t have a prototype chain to begin with.

This one. Basically, the chain works backwards. The actual object you are checking (the object passed into isPrototypeOf) is at the very end of the chain. You then work your way backwards (“up” the chain) to figure out if Object is somewhere in the chain. Object is the default starting point for all objects in JS, so unless you intentionally do something to remove it from the chain, Object.prototype.isPrototypeOf will return true for any object.

I agree that the name of this function can be a little challenging. Let’s look at the example in the challenge:

Bird.prototype.isPrototypeOf(duck);

This is asking “Is Bird a prototype of duck?” If you ignore prototype then you have

Bird.isPrototypeOf(duck);

Which almost sounds like “Is Bird a prototype of duck?” I suppose you could literally read it as “Bird is prototype of duck?” and make it sound like a question with the proper inflection. Not sure if this helps but that’s sort of how I think of it.

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