Understand the Prototype Chain - Is Object.prototype a prototype for all JS objects?

Here is my code:


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

let beagle = new Dog("Snoopy");

Dog.prototype.isPrototypeOf(beagle);  // => true

Object.prototype.isPrototypeOf(Dog.prototype);

Based off of this i understand that Dog.prototype is the prototype of all of the objects that have been constructed by it, for instance, beagle.

Now i am saying Object.prototype is the prototype of the prototype all of those objects. This means that all JavaScript objects across all the code will share this prototype? Meaning i could store a function in it? Number of legs? How do i even edit it, Like a regular prototype?

A super-type is the object, and the created object is the subtype. Is the prototype also considered the super-type?

Your browser information:

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

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/object-oriented-programming/understand-the-prototype-chain

Yes. Think of it as a chain - your current object links through its prototype to the class from which it was created, and that ones parents’ prototype, allthe way back to the base Object.prototype

And you CAN do the following:

Object.prototype.howManyLegs = function(){...}

But unless it is a function that should be in all objects, don’t. An example might be to replace the default Object.toString() with something more meaningful.

For functions you want to include in all your objects, i might create a class that contains just the ‘universal’ functions, then extend that.

1 Like

Could you type that out like a regular prototype?