In JavaScript, every object has an internal hidden property called [[Prototype]]
, which refers to another object called its prototype. A prototype is essentially a blueprint or template from which objects inherit properties and methods. This prototype-based inheritance allows objects to share functionality and makes JavaScript more dynamic and flexible.
- Prototype: An object that acts as a fallback for another object. When a property or method is not found directly on an object, JavaScript looks for it in the object's prototype chain.
- Prototype Chain: A chain of objects connected via their
[[Prototype]]
. If the property or method is not found in the current object, JavaScript traverses up the prototype chain until it finds the property or reaches the end (null
).
- Code Reuse: Prototypes allow shared methods and properties, reducing memory usage.
- Inheritance: Objects can inherit behavior from other objects without duplicating code.
- Dynamic Behavior: You can modify an object's prototype at runtime to add new properties or methods.
-
Object.getPrototypeOf()
: Retrieves the prototype of an object.let obj = {}; console.log(Object.getPrototypeOf(obj)); // Output: {}
-
Object.prototype
: The top-level prototype in the chain.console.log(Object.prototype); // Output: {}
-
__proto__
(deprecated but still widely used): Refers to the[[Prototype]]
of an object.let obj = {}; console.log(obj.__proto__ === Object.prototype); // Output: true
All objects by default inherit from Object.prototype
, which provides built-in methods like toString()
, valueOf()
, and more.
let obj = {};
console.log(obj.toString()); // Output: "[object Object]"
You can add properties or methods to an object's prototype to share functionality.
function Person(name) {
this.name = name;
}
Person.prototype.greet = function () {
console.log("Hello, my name is " + this.name);
};
let alice = new Person("Alice");
let bob = new Person("Bob");
alice.greet(); // Output: "Hello, my name is Alice"
bob.greet(); // Output: "Hello, my name is Bob"
When a property or method is accessed, JavaScript checks:
- The object itself.
- The object's prototype.
- The next object in the prototype chain, continuing until it finds the property or reaches
null
.
let obj = {
name: "Alice"
};
console.log(obj.name); // Output: "Alice" (found in the object itself)
console.log(obj.toString()); // Output: "[object Object]" (found in Object.prototype)
If the property or method is not found in the prototype chain, JavaScript returns undefined
.
You can set a custom prototype using Object.create()
.
let animal = {
speak: function () {
console.log(this.name + " makes a sound.");
}
};
let dog = Object.create(animal);
dog.name = "Buddy";
dog.speak(); // Output: "Buddy makes a sound."
prototype
: A property of constructor functions (e.g.,Person.prototype
) that sets the prototype of objects created by that constructor.__proto__
: An internal reference that links an object to its prototype.
function Person(name) {
this.name = name;
}
let john = new Person("John");
console.log(Person.prototype); // The prototype object of Person
console.log(john.__proto__); // The prototype object of john
console.log(john.__proto__ === Person.prototype); // Output: true
Objects can inherit from other objects via prototypes, enabling the reuse of properties and methods.
function Animal(name) {
this.name = name;
}
Animal.prototype.eat = function () {
console.log(this.name + " is eating.");
};
function Dog(name) {
Animal.call(this, name);
}
Dog.prototype = Object.create(Animal.prototype);
Dog.prototype.bark = function () {
console.log(this.name + " is barking.");
};
let rex = new Dog("Rex");
rex.eat(); // Output: "Rex is eating." (inherited from Animal)
rex.bark(); // Output: "Rex is barking." (defined in Dog)
- Memory Efficiency: Shared methods are stored in one location (prototype) rather than in every instance.
- Dynamic Behavior: You can extend prototypes at runtime.
- Supports Inheritance: Simplifies creating hierarchies and sharing behavior across objects.
-
Array.prototype
- Methods like
map()
,filter()
,reduce()
are part ofArray.prototype
.
- Methods like
-
String.prototype
- Methods like
toUpperCase()
,slice()
,replace()
are part ofString.prototype
.
- Methods like
-
Function.prototype
- Methods like
bind()
,call()
,apply()
are part ofFunction.prototype
.
- Methods like
The prototype in JavaScript is a powerful feature that enables inheritance, code reuse, and dynamic extension of functionality. Understanding prototypes and the prototype chain is essential for working with JavaScript effectively and designing efficient, maintainable applications.