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)

Cloudinary image

Optimize, customize, deliver, manage and analyze your images.

Remove background in all your web images at the same time, use outpainting to expand images with matching content, remove objects via open-set object detection and fill, recolor, crop, resize... Discover these and hundreds more ways to manage your web images and videos on a scale.

Learn more