DEV Community

Laxman Nemane
Laxman Nemane

Posted on

JavaScript's Prototype-Based Inheritance Example

Revisit the fundamental concept of prototype-based inheritance and unlock the full potential of JavaScript. Discover how objects can inherit properties and methods from their prototypes and simplify your code. Take your development skills to the next level!

`
// Create a simple object
let animal = {
sound: function() {
console.log("The animal makes a sound");
}
};

// Create a new object that inherits from animal
let dog = Object.create(animal);
dog.sound(); // Output: The animal makes a sound

// Add a new method to the dog object
dog.bark = function() {
console.log("The dog barks");
};

// Create a new object that inherits from dog
let myDog = Object.create(dog);
myDog.bark(); // Output: The dog barks
myDog.sound(); // Output: The animal makes a sound
`

Top comments (0)