DEV Community

Laxman Nemane
Laxman Nemane

Posted on

1

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)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay