JavaScript > Prototypes and Inheritance > Prototype Chain > Understanding prototypes
Understanding JavaScript Prototypes
This example demonstrates how prototypes work in JavaScript, showcasing inheritance and the prototype chain.
Basic Prototype Example
This code defines a constructor function `Animal`. A method `sayName` is added to `Animal.prototype`. When `animal1` is created using `new Animal()`, it inherits the `sayName` method from `Animal.prototype`. This demonstrates that objects created from the same constructor share the same prototype, preventing method duplication in memory. The last console.log shows that the prototype of the animal1 is the Animal.prototype.
// Define a constructor function
function Animal(name) {
this.name = name;
}
// Add a method to the Animal prototype
Animal.prototype.sayName = function() {
return 'My name is ' + this.name;
};
// Create an instance of Animal
const animal1 = new Animal('Dog');
// Access the method
console.log(animal1.sayName()); // Output: My name is Dog
//Show prototype
console.log(Object.getPrototypeOf(animal1) === Animal.prototype); //true
Prototype Chain Example
This code demonstrates the prototype chain. `Dog` inherits from `Animal`. `Dog.prototype = Object.create(Animal.prototype)` sets up the inheritance. Now, `dog1` can access methods from both `Dog.prototype` (`bark`) and `Animal.prototype` (`sayName`). The prototype chain is `dog1 -> Dog.prototype -> Animal.prototype -> Object.prototype -> null`. The `instanceof` operator confirm the inheritance.
// Define a constructor function
function Animal(name) {
this.name = name;
}
Animal.prototype.sayName = function() {
return 'My name is ' + this.name;
};
// Define another constructor function inheriting from Animal
function Dog(name, breed) {
Animal.call(this, name); // Call Animal constructor to set name
this.breed = breed;
}
// Set Dog's prototype to Animal's prototype
Dog.prototype = Object.create(Animal.prototype);
// Optionally, set the constructor property back to Dog
Dog.prototype.constructor = Dog;
// Add a method specific to Dog
Dog.prototype.bark = function() {
return 'Woof!';
};
// Create an instance of Dog
const dog1 = new Dog('Buddy', 'Golden Retriever');
// Access methods from both Dog and Animal prototypes
console.log(dog1.sayName()); // Output: My name is Buddy
console.log(dog1.bark()); // Output: Woof!
//Check inheritance
console.log(dog1 instanceof Animal); //true
console.log(dog1 instanceof Dog); //true
Concepts Behind the Snippet
Prototypes are the mechanism by which JavaScript objects inherit features from one another. Every object in JavaScript has a prototype, which is another object. When you try to access a property of an object, JavaScript first looks in the object itself. If it doesn't find the property, it looks in the object's prototype, and so on up the prototype chain until it reaches `null`. This is the essence of prototypal inheritance.
Real-Life Use Case
Consider a library for creating UI components. You might have a base `Component` class with common methods like `render` and `update`. Specific components like `Button` and `TextBox` could inherit from `Component`, adding their own specific rendering logic. This avoids code duplication and promotes code reuse.
Best Practices
Interview Tip
Be prepared to explain the difference between classical inheritance (as in languages like Java) and prototypal inheritance (in JavaScript). Also, be ready to draw a diagram of the prototype chain for a given code example.
When to Use Prototypes
Prototypes are fundamental to JavaScript object-oriented programming. Use them whenever you need to create objects that share common properties and methods, or when you need to implement inheritance hierarchies. They are particularly helpful for creating reusable components and libraries.
Memory Footprint
Prototypes are memory-efficient because methods are stored on the prototype object, rather than being duplicated for each instance. This can significantly reduce memory consumption, especially when creating many objects with similar functionality.
Alternatives
Pros
Cons
FAQ
-
What is the prototype chain?
The prototype chain is the mechanism by which JavaScript objects inherit properties from one another. When you try to access a property of an object, JavaScript first looks in the object itself. If it doesn't find the property, it looks in the object's prototype, and so on up the chain until it reaches `null`. -
How do I set the prototype of an object?
You can set the prototype of an object using `Object.create(prototypeObject)`. This creates a new object with the specified prototype. -
What is the difference between `instanceof` and `Object.getPrototypeOf()`?
`instanceof` checks if an object is an instance of a constructor, meaning whether the constructor's prototype is in the object's prototype chain. `Object.getPrototypeOf()` returns the actual prototype object of an object.