In JavaScript, every object has a prototype which acts as a blueprint for that object. This prototype can be used to add properties and methods to an object or to inherit properties and methods from another object.
The prototype chain is a mechanism that allows the objects to inherit properties and methods from their prototypes. When an object tries to access a property or method that is not defined on itself, JavaScript looks for it in the object’s prototype. If the property or method is still not found, it continues to look for it in the prototype of the prototype until it reaches the end of the chain.
Example
Let’s create a basic example to illustrate the prototype chain:
// Create a parent object
var parent = {
name: 'John',
sayHello: function() {
console.log('Hello, my name is ' + this.name);
}
};
// Create a child object
var child = Object.create(parent);
child.name = 'Jane';
// Call the sayHello method on the child object
child.sayHello(); // Output: Hello, my name is Jane
In this example, we create a parent
object with a name
property and a sayHello
method. We then create a child
object using the Object.create
method and set its name
property to 'Jane'
.
When we call the sayHello
method on the child
object, JavaScript first looks for it on the child
object itself. Since it’s not defined on the child
object, it continues to look for it in the child
object’s prototype, which is the parent
object. The sayHello
method is found in the parent
object, and it is executed with the child
object’s name
property.
Object.prototype
All objects in JavaScript inherit properties and methods from the Object.prototype
object. This includes the toString
method, which is used to convert an object to a string.
var obj = {};
console.log(obj.toString()); // Output: [object Object]
In this example, we create an empty object obj
and call its toString
method. Since obj
does not have a toString
method of its own, JavaScript looks for it in obj
‘s prototype, which is Object.prototype
. The toString
method is found in Object.prototype
and executed with obj
as its context.
Function.prototype
Functions in JavaScript also have a prototype, which is used to create new objects with the same properties and methods. This is commonly used for object inheritance in JavaScript.
function Person(name) {
this.name = name;
}
Person.prototype.sayHello = function() {
console.log('Hello, my name is ' + this.name);
}
var john = new Person('John');
john.sayHello(); // Output: Hello, my name is John
var jane = new Person('Jane');
jane.sayHello(); // Output: Hello, my name is Jane
In this example, we create a Person
function that takes a name
argument and sets it as a property on the newly created object. We then add a sayHello
method to the Person.prototype
object.
We create two new Person
objects, john
and jane
, and call their sayHello
methods. Since the sayHello
method is not defined on the john
and jane
objects themselves, JavaScript looks for it in their prototype, which is Person.prototype
. The sayHello
method is found in Person.prototype
and executed with the john
and jane
objects as their context.
Conclusion
The prototype chain is an important concept in JavaScript that allows objects to inherit properties and methods from their prototypes. Understanding how it works is essential for writing efficient and effective JavaScript code. By using the prototype chain, you can create complex inheritance structures and avoid duplicating code across your application.