Published on

Inheritance and Prototype Chain

Authors

Table of Contents:

Introduction

Inheritance is a fundamental concept in object-oriented programming that allows objects to inherit properties and methods from other objects. JavaScript, being a prototype-based language, implements inheritance through the prototype chain. In this guide, we will delve into inheritance and the prototype chain in JavaScript and understand how they enable code reuse and object relationships. Let's get started!

Prototype Chain

The prototype chain is a mechanism in JavaScript that defines the inheritance hierarchy between objects. Every object in JavaScript has an internal [[Prototype]] property that points to its prototype object. When accessing a property or method on an object, JavaScript first looks for it on the object itself. If it's not found, it traverses up the prototype chain until the property is found or until the end of the chain is reached (i.e., the prototype is null).

// Example
const person = {
  name: 'John',
  greet() {
    console.log('Hello, ' + this.name)
  },
}

const employee = {
  role: 'Developer',
  __proto__: person,
}

employee.greet() // Output: Hello, John
console.log(employee.name) // Output: John

In this example, the person object acts as the prototype of the employee object. When we call the greet method on the employee object, JavaScript looks for it in the employee object itself. Since it's not found, JavaScript continues the search up the prototype chain and finds the greet method in the person object.

Prototype-Based Inheritance

JavaScript implements inheritance using a prototype-based model, which differs from the classical class-based inheritance found in languages like Java or C++. In prototype-based inheritance, objects directly inherit from other objects, known as prototypes, rather than classes.

// Example
function Person(name) {
  this.name = name
}

Person.prototype.greet = function () {
  console.log('Hello, ' + this.name)
}

function Employee(name, role) {
  Person.call(this, name)
  this.role = role
}

Employee.prototype = Object.create(Person.prototype)
Employee.prototype.constructor = Employee

const john = new Employee('John', 'Developer')
john.greet() // Output: Hello, John
console.log(john.name) // Output: John

In this example, we have a Person constructor function and an Employee constructor function. The Employee object inherits from the Person object by setting its prototype to Object.create(Person.prototype). This establishes the prototype chain, allowing the Employee object to access properties and methods defined in the Person object.

Closing Thoughts

Understanding inheritance and the prototype chain is crucial for building robust and reusable JavaScript code. By leveraging the power of prototype-based inheritance, you can create flexible and maintainable object relationships.

Top 5 Resources

  1. MDN Web Docs - Inheritance and the prototype chain
  2. JavaScript.info - Prototypes, inheritance
  3. W3Schools - JavaScript Object Prototypes
  4. Exploring ES6 - Classes and Inheritance

Continue exploring and experimenting with inheritance and the prototype chain to deepen your understanding of JavaScript object relationships and improve your coding skills.

Happy coding!