prototype in JavaScript
Definition:
“In JavaScript, a prototype is an object from which other objects inherit properties and methods. Every JavaScript function has a prototype
property that is used to establish inheritance, allowing objects to share behavior efficiently. This forms the basis of JavaScript's prototype-based inheritance model."
Key Points for Interviews:
- Every function in JavaScript has a
prototype
property, which is an object. - Objects inherit properties and methods from their prototype using the
[[Prototype]]
(also called__proto__
). - Prototype chaining allows searching for properties up the chain if they are not found on the object itself.
- Helps achieve inheritance and code reusability without using traditional classes (before ES6).
Example to Explain in an Interview:
function Person(name) {
this.name = name;
}
// Adding a method to the prototype
Person.prototype.greet = function() {
return `Hello, my name is ${this.name}`;
};
let user1 = new Person("Alice");
console.log(user1.greet()); // Output: Hello, my name is Alice
Here, user1
does not have a greet
method directly, but it inherits it from Person.prototype
.
Prototype Chaining & Classical Inheritance: Key Differences
JavaScript uses prototype-based inheritance, whereas languages like Java or C++ use classical inheritance (also called class-based inheritance). Let’s break it down in a structured way for interview purposes.
1. Prototype Chaining in JavaScript
Prototype chaining is the process where an object inherits properties and methods from its prototype, and if the property/method is not found, JavaScript continues looking up the chain.
Example of Prototype Chaining
function Animal(name) {
this.name = name;
}
// Adding a method to the prototype
Animal.prototype.speak = function() {
return `${this.name} makes a sound`;
};
function Dog(name, breed) {
Animal.call(this, name); // Call Animal constructor
this.breed = breed;
}
// Setting Dog's prototype to inherit from Animal
Dog.prototype = Object.create(Animal.prototype);
Dog.prototype.constructor = Dog;
// Adding a new method to Dog prototype
Dog.prototype.bark = function() {
return `${this.name} barks loudly!`;
};
let dog1 = new Dog("Buddy", "Golden Retriever");
console.log(dog1.speak()); // Inherited from Animal -> "Buddy makes a sound"
console.log(dog1.bark()); // Defined in Dog -> "Buddy barks loudly!"
💡 How does prototype chaining work here?
dog1
first looks forspeak()
in its own properties.- Since
speak()
is not found inDog.prototype
, it moves up toAnimal.prototype
. - This process continues until it reaches
Object.prototype
(the root prototype). If still not found, it returnsundefined
.
Key Takeaways for Interviews
✅ Prototype Chaining: If a property is not found in an object, JavaScript looks up its prototype chain.
✅ Prototype-Based Inheritance: Objects inherit directly from other objects via prototypes (Object.create()
).
✅ Classical vs. Prototype Inheritance: Classical inheritance copies properties, while prototype inheritance shares them.
✅ ES6 Classes Are Still Prototypes: class
syntax is just a cleaner way to use prototypes in JavaScript.
Would you like me to explain any part in more detail or provide a trickier interview question? 😊