JavaScript Advanced Prototypes & Prototype Inheritance

Learn JavaScript Advanced Prototypes & Prototype Inheritance with practical examples, explanations, best practices, and real-world coding techniques.

Try Editor

Prototypes are one of the most important internal concepts in JavaScript. They form the foundation of object inheritance and power many features of the language.

Unlike many traditional programming languages, JavaScript uses prototype-based inheritance instead of classical inheritance.

Understanding prototypes helps developers learn:

  • Object inheritance
  • Method sharing
  • JavaScript internals
  • Classes
  • Memory optimization

Prototypes are a foundational part of how JavaScript objects work internally.

What is a Prototype?

In JavaScript, every object has a hidden internal property called a prototype.

A prototype is another object from which properties and methods can be inherited.

This allows objects to share functionality without duplicating code.

Basic Prototype Example

JavaScript objects can inherit properties from other objects.


const animal = {

  eats: true

};

const dog = Object.create(animal);

dog.barks = true;

console.log(dog.eats);
      

Output:


true
      

The dog object inherits the eats property from animal.

Prototype Chain

When JavaScript cannot find a property directly on an object, it searches the prototype chain.

This continues until:

  • The property is found
  • Or the chain ends with null

This mechanism is called prototype chaining.

How Prototype Lookup Works


dog → animal → Object.prototype → null
      

JavaScript checks each object in the chain until the property is found.

This allows shared behavior across multiple objects.

Constructor Functions and Prototypes

Before ES6 classes, constructor functions were commonly used to create reusable object blueprints.


function User(name) {

  this.name = name;

}

User.prototype.greet = function() {

  console.log(
    `Hello ${this.name}`
  );

};

const user1 =
  new User("Alex");

user1.greet();
      

The greet() method is shared through the prototype instead of being recreated for every object.

Why Prototypes Matter

Prototypes improve memory efficiency because shared methods are stored once instead of being duplicated.

This becomes extremely important in large applications with many objects.

Prototype Inheritance

Prototype inheritance allows one object to inherit functionality from another object.

This creates reusable and extendable object structures.


function Animal(name) {

  this.name = name;

}

Animal.prototype.walk = function() {

  console.log(
    `${this.name} is walking`
  );

};

function Dog(name) {

  Animal.call(this, name);

}

Dog.prototype =
  Object.create(Animal.prototype);

Dog.prototype.bark = function() {

  console.log("Woof");

};

const pet =
  new Dog("Rocky");

pet.walk();
pet.bark();
      

The Dog object inherits methods from Animal.

Built-In JavaScript Prototypes

JavaScript internally uses prototypes for built-in objects like:

  • Array
  • String
  • Date
  • Function

For example, array methods such as map() and filter() exist on Array.prototype.

ES6 Classes and Prototypes

Modern JavaScript classes are built on top of prototypes internally.

Classes provide cleaner syntax, but prototype inheritance still powers the underlying behavior.


class User {

  constructor(name) {

    this.name = name;

  }

  greet() {

    console.log(
      `Hello ${this.name}`
    );

  }

}
      

The greet() method is still stored on the prototype internally.

Inspecting Prototypes

Developers can inspect prototypes using:

  • Object.getPrototypeOf()
  • __proto__

const user = {

  name: "Alex"

};

console.log(
  Object.getPrototypeOf(user)
);
      

Common Beginner Mistakes

  • Confusing prototypes with classes
  • Thinking methods are copied between objects
  • Misunderstanding the prototype chain
  • Overusing manual prototype manipulation

Best Practices

  • Use ES6 classes for cleaner syntax in modern applications
  • Understand prototype behavior internally
  • Avoid modifying built-in prototypes unnecessarily
  • Use inheritance carefully to keep architecture maintainable

Why Prototype Inheritance Matters

Prototype inheritance is one of JavaScript's defining features.

It powers:

  • Classes
  • Frameworks
  • Object reuse
  • Method sharing

Understanding prototypes provides deep insight into how JavaScript actually works internally.

Summary

Prototypes are the foundation of inheritance in JavaScript. Every object can inherit properties and methods through the prototype chain, enabling reusable and memory-efficient code structures.

Understanding prototype inheritance is essential for mastering advanced JavaScript architecture and object-oriented programming concepts.

Code Examples

js

const animal = {

  eats: true

};

const dog = Object.create(animal);

dog.barks = true;

console.log(dog.eats);

    
js

function User(name) {

  this.name = name;

}

User.prototype.greet = function() {

  console.log(
    `Hello ${this.name}`
  );

};

const user1 =
  new User("Alex");

user1.greet();

    
js

class User {

  constructor(name) {

    this.name = name;

  }

  greet() {

    console.log(
      `Hello ${this.name}`
    );

  }

}

    

Pro Tip

Even modern JavaScript classes still use prototypes internally. Classes are mainly cleaner syntax built on top of prototype inheritance.

🎨 IgKnight Docs

Master HTML, CSS, JS, Python, .NET and more with easy-to-follow lessons and practical code examples.

© 2025 IgKnight Learning Platform. All rights reserved.