A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/javascript/javascript-inheritance/ below:

JavaScript Inheritance - GeeksforGeeks

JavaScript Inheritance

Last Updated : 06 Aug, 2025

Inheritance is a fundamental concept in object-oriented programming (OOP) that allows one class or object to derive properties and behaviours from another.

In the following example, Animal is the base class and Dog, Cat and Cow are derived classes that extend the Animal class.

Think of inheritance in JavaScript like a family. Just like kids inherit traits from their parents, objects in JavaScript can inherit properties and actions from other objects. This means we don’t have to write the same code over and over again.

JavaScript does this using prototypes, which connect objects in a chain, allowing them to share features effortlessly. This makes coding easier, cleaner, and more efficient—just like learning from those who came before us.

Common Types of Inheritance 1. prototype-based inheritance

In JavaScript, everything (functions, arrays, strings) is an object. The prototype stores shared properties and methods, allowing all instances of a type to access them.

JavaScript
function Animal(name) {
    this.name = name;
}
Animal.prototype.speak = function () {
    console.log(`${this.name} makes a sound.`);
};

// Child constructor function
function Dog(name) {
    Animal.call(this, name); // Inherit properties
}

// Inherit methods from Animal
Dog.prototype = Object.create(Animal.prototype);
Dog.prototype.constructor = Dog;

// Adding a new method to Dog
Dog.prototype.bark = function () {
    console.log(`${this.name} barks: Woof!`);
};

// Creating an instance
const myDog = new Dog("Buddy");

myDog.speak(); 
myDog.bark();  

Output
Buddy makes a sound.
Buddy barks: Woof!

To learn more about prototypes you can refer JavaScript Prototypes

2. ES6 Class-based Inheritance

This code demonstrates class inheritance in JavaScript, where a child class (two) extends a parent class (one). The child class inherits properties and methods from the parent class.

JavaScript
class one {
    constructor(name) {
        this.name = name
    }
    speaks() {
        return `my name is ${this.name}`
    }
}
class two extends one {
    constructor(name) {
        super(name)
    }
}
const o = new two('Pranjal')
console.log(o.speaks())

Output
my name is Pranjal
3. Mixins for Inheritance

This code demonstrates prototypal inheritance and object merging using Object.assign(), allowing a constructor function (Person) to inherit methods from multiple objects (one and two).

JavaScript
const one = {
    speak() {
        return `${this.name} walks`
    }
}
const two = {
    walks() {
        return `${this.name} walks`
    }
}
function Person(name) {
    this.name = name
}
Object.assign(Person.prototype, one, two)
const person1 = new Person('Pranjal')
console.log(person1.speak())
console.log(person1.walks())

Output
Pranjal walks
Pranjal walks
4. Inheritance with Object.create()

Object.create() in JavaScript creates a new object that uses another object as its prototype, allowing it to inherit all its properties and methods.

JavaScript
let obj = {
    name: 'Pranjal',
    age: 21,
    prints() {
        return `my name is ${this.name}`
    }
}
let obj1 = Object.create(obj)
obj1.name = 'Hello'
console.log(obj1.age)
console.log(obj1.prints())

Output
21
my name is Hello
5. Inheritance with object.setPrototypeOf()

This code demonstrates prototypal inheritance using Object.setPrototypeOf(), which sets one object (two) as the prototype of another (one). This allows one to access properties from two.

JavaScript
const one = {
    speak() {
        return `${this.name} speaks`
    }
}
const two = {
    name: 'Pranjal'
}
Object.setPrototypeOf(one, two)
console.log(one.speak())
6. Factory functions for inheritance

This code demonstrates factory functions in JavaScript, where a function (createPerson) returns new objects with shared properties and methods.

JavaScript
function createPerson(name) {
    return {
        name: name,
        greet() {
            return `Hello my name is ${this.name}`
        }
    }
}
const one = createPerson('Pranjal')
const two = createPerson('Pranav')
console.log(one.greet())
console.log(two.greet())

Output
Hello my name is Pranjal
Hello my name is Pranav


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4