Facebook Pixel

Prototypes and Prototypal Inheritance in JavaScript

How objects inherit from prototypes in JavaScript. Here is the complete guide.

Prototypes and Prototypal Inheritance in JavaScript

JavaScript uses prototypal inheritance: objects inherit from other objects via the prototype chain.

What Is a Prototype?

Every object has a hidden [[Prototype]] property that points to another object (its prototype). When you access a property, JS checks the object first, then its prototype, then the prototype's prototype, up to null.

The Prototype Chain

const animal = { eat: () => "eating" }; const dog = Object.create(animal); dog.bark = () => "barking"; dog.eat(); // "eating" (inherited from animal) dog.bark(); // "barking" (own property)

Constructor Functions

function Person(name) { this.name = name; } Person.prototype.greet = function () { return `Hello, ${this.name}`; }; const p = new Person("Kunal"); p.greet(); // "Hello, Kunal" (inherited from Person.prototype)

ES6 Classes

class Person { constructor(name) { this.name = name; } greet() { return `Hello, ${this.name}`; } } class Student extends Person { constructor(name, grade) { super(name); this.grade = grade; } }

Key Methods

  • Object.create(proto): create an object with a specific prototype.
  • Object.getPrototypeOf(obj): get the prototype.
  • Object.setPrototypeOf(obj, proto): set the prototype.
  • obj.hasOwnProperty(key): check if a property is own (not inherited).
  • obj instanceof Constructor: check the prototype chain.

The Takeaway

Prototypal inheritance: objects inherit from objects via the prototype chain. Use Object.create for direct inheritance, constructor functions with Person.prototype, or ES6 classes (extends). The prototype chain is how JS resolves properties.

Objects inherit from other objects via the prototype chain. When you access a property, JS checks the object, then its prototype, then the prototype's prototype, up to null. This is different from class-based inheritance in Java/C++.

Use Object.create(proto): const dog = Object.create(animal). dog inherits all properties from animal. You can also add own properties: Object.create(animal, { bark: { value: () => 'barking' } }).

__proto__ is the actual prototype of an instance (on every object). prototype is on constructor functions and is the object that instances will inherit from. new Person() creates an object whose __proto__ is Person.prototype.

ES6 classes are syntactic sugar over prototypes. class Person { greet() {} } puts greet on Person.prototype. extends creates the prototype chain. Under the hood, it is the same prototypal inheritance.

The chain of objects that JS traverses to find a property. obj -> obj.__proto__ -> obj.__proto__.__proto__ -> ... -> null. When you access obj.property, JS checks each link until it finds it or reaches null.

Ready to master React completely?

Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.

Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.