Prototypal Inheritance and Classes.

PHOTO EMBED

Tue Apr 22 2025 19:50:39 GMT+0000 (Coordinated Universal Time)

Saved by @signup

Prototypal Inheritance (Old Way)

function Animal(name) {
    this.name = name;
}

Animal.prototype.sayHello = function() {
    console.log("Hello, I'm " + this.name);
};

let dog = new Animal("Dog");
dog.sayHello(); // Output: Hello, I'm Dog




 Classes (New Way – ES6)

class Animal {
    constructor(name) {
        this.name = name;
    }

    sayHello() {
        console.log(`Hello, I'm ${this.name}`);
    }
}

let cat = new Animal("Cat");
cat.sayHello(); // Output: Hello, I'm Cat
content_copyCOPY