Member-only story
Prototypal inheritance is a fundamental concept in JavaScript that allows objects to inherit properties and methods from other objects. It’s a powerful feature that enables code reuse and helps create more efficient and organized code. One way to leverage prototypal inheritance is through the Object.create()
method.
In this article, we'll explore Object.create()
and how it can be used to implement prototypal inheritance in JavaScript.
First, let’s understand what Object.create()
does. It creates a new object with a specified prototype object and properties. The syntax is:
Object.create(proto, propertiesObject)
The proto
parameter is the object that should be used as the prototype of the newly created object. The propertiesObject
parameter is optional and defines properties to be added to the newly created object.
Here’s a simple example:
const animal = {
walk() {
console.log("Walking...");
}
};
const dog = Object.create(animal);
dog.walk(); // Output: "Walking..."