JavaScript prototype chain | Simple

  • by

In JavaScript, every object has a prototype, including the prototype object. This “chain” goes all the way back until it reaches an object that has no prototype, usually Object‘s a prototype. Because each object has a private property that holds a link to another object that’s why it is called a prototype chain in JavaScript.

function Dog(name) {
  this.name = name;
}
Object.prototype.isPrototypeOf(Dog.prototype);

The prototype is basically a property of a JavaScript function. The prototype’s version of “Inheritance” involves adding another link to the end of this prototype chain, as shown above.

JavaScript prototype chain

Simple example code.

<!DOCTYPE html>
<html>
<body>
  <script>
    const myObject = {
      city: 'Madrid',
      greet() {
        console.log(`Greetings from ${this.city}`);
      }
    }

    myObject.greet();
    console.log(myObject.toString())
  </script>
</body>
</html> 

Output:

JavaScript prototype chain

Read more: https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Object_prototypes

Do comment if you have any doubt or suggestions on this Js prototype topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.