JavaScript Prototype
JavaScript Prototype
JavaScript Prototype
Donate Now
(https://www.javascripttutorial.net/donation/)
typeof(Object)
Output:
'function'
https://www.javascripttutorial.net/javascript-prototype/ 1/12
29/07/2022, 08:09 JavaScript Prototype
Please note that Object() is a function, not an object. It’s confusing if this is the first time
you’ve learned about the JavaScript prototype.
console.log(Object.prototype);
(https://www.javascripttutorial.net/wp-content/uploads/2021/01/JavaScript-Prototype-Object.prototype.png)
Note when a function is a value of an object property, it’s called a method. Therefore, a method
is an object property with value as a function.
The Object.prototype also has an important property called constructor that references the
Object() function.
The following statement confirms that the Object.prototype.constructor property references the
Object function:
https://www.javascripttutorial.net/javascript-prototype/ 2/12
29/07/2022, 08:09 JavaScript Prototype
Suppose a circle represents a function and a square represents an object. The following picture
illustrates the relationship between the Object() function and the Object.prototype object:
.constructor
constructor
.prototype toString()
Object
valueOf()
...
(https://www.javascripttutorial.net/wp-
content/uploads/2022/01/JS-prototype.svg)
function Person(name) {
this.name = name;
In this example, the Person() function accepts a name argument and assigns it to the name
property of the this object.
Behind the scenes, JavaScript creates a new function Person() and an anonymous object:
.constructor
.prototype
Person constructor
(https://www.javascripttutorial.net/wp-
content/uploads/2022/01/JS-prototype-Person-type.svg)
Like the Object() function, the Person() function has a property called prototype that
references an anonymous object. And the anonymous object has the constructor property that
references the Person() function.
https://www.javascripttutorial.net/javascript-prototype/ 3/12
29/07/2022, 08:09 JavaScript Prototype
The following shows the Person() function and the anonymous object referenced by the
Person.prototype :
console.log(Person);
console.log(Person.prototype);
(https://www.javascripttutorial.net/wp-content/uploads/2021/01/JavaScript-
Prototype-Person-function.png)
In addition, JavaScript links the Person.prototype object to the Object.prototype object via the
[[Prototype]] , which is known as a prototype linkage.
.constructor
constructor
.prototype toString()
Object
valueOf()
...
.constructor
.prototype [[Prototype]]
Person constructor
https://www.javascripttutorial.net/javascript-prototype/ 4/12
29/07/2022, 08:09 JavaScript Prototype
(https://www.javascripttutorial.net/wp-content/uploads/2022/01/JS-prototype-Person-prototype.svg)
The following defines a new method called greet() in the Person.prototype object:
Person.prototype.greet = function() {
In this case, the JavaScript engine adds the greet() method to the Person.prototype object:
.constructor
constructor
.prototype toString()
Object
valueOf()
...
.constructor
(https://www.javascripttutorial.net/wp-content/uploads/2022/01/JS-prototype-method.svg)
Internally, the JavaScript engine creates a new object named p1 and links the p1 object to the
Person.prototype object via the prototype linkage:
https://www.javascripttutorial.net/javascript-prototype/ 5/12
29/07/2022, 08:09 JavaScript Prototype
.constructor
constructor
.prototype toString()
Object
valueOf()
...
.constructor
[[Prototype]]
name: 'John'
p1
(https://www.javascripttutorial.net/wp-content/uploads/2022/01/JS-prototype-Person-object.svg)
console.log(greeting);
Because p1 doesn’t have the greet() method, JavaScript follows the prototype linkage and finds it
on the Person.prototype object.
Since JavaScript can find the greet() method on the Person.prototype object, it executes the
greet() method and returns the result:
let s = p1.toString();
console.log(s);
In this case, the JavaScript engine follows the prototype chain to look up for the toString() method
in the Person.prototype .
Because the Person.prototype doesn’t have the toString() method, the JavaScript engine goes
up to the prototype chain and searches for the toString() method in the Object.prototype
object.
Since JavaScript can find the toString() method in the Object.prototype , it executes the
toString() method.
.constructor
constructor
.prototype toString()
Object
valueOf()
...
.constructor
[[Prototype]]
p1.toString()
name: 'John'
p1
(https://www.javascripttutorial.net/wp-content/uploads/2022/01/JS-prototype-calling-a-method.svg)
https://www.javascripttutorial.net/javascript-prototype/ 7/12
29/07/2022, 08:09 JavaScript Prototype
If you call a method that doesn’t exist on the Person.prototype and Object.prototype object,
the JavaScript engine will follow the prototype chain and throw an error if it cannot find the method.
For example:
p1.fly();
Because the fly() method doesn’t exist on any object in the prototype chain, the JavaScript engine
issues the following error:
The following creates another instance of the Person whose name property is 'Jane' :
(https://www.javascripttutorial.net/wp-content/uploads/2022/01/JS-prototype-two-person-objects.svg)
https://www.javascripttutorial.net/javascript-prototype/ 8/12
29/07/2022, 08:09 JavaScript Prototype
In conclusion, when you define a method on the prototype object, this method is shared by all
instances.
p1.draw = function () {
};
The JavaScript engine adds the draw() method to the p1 object, not the Person.prototype
object:
(https://www.javascripttutorial.net/wp-content/uploads/2022/01/JS-prototype-object-with-method.svg)
It means that you can call the draw() method on the p1 object:
p1.draw();
https://www.javascripttutorial.net/javascript-prototype/ 9/12
29/07/2022, 08:09 JavaScript Prototype
p2.draw()
Error:
When you define a method in an object, the method is only available to that object. It cannot be
shared with other objects by default.
production code.
The p1.__proto__ exposes the [[Prototype]] that references the Person.prototype object.
As mentioned earlier, you should use the Object.getPrototypeOf() method instead of the
__proto__ .
https://www.javascripttutorial.net/javascript-prototype/ 10/12
29/07/2022, 08:09 JavaScript Prototype
Another popular way to get the prototype linkage is when the Object.getPrototypeOf() method is
not available is via the constructor property as follows:
p1.constructor.prototype
Shadowing
console.log(p1.greet());
The p1 object doesn’t have the greet() method defined, therefore JavaScript goes up to the
prototype chain to find it. In this case, it can find the method in the Person.prototype object.
Let’s add a new method to the object p1 with the same name as the method in the
Person.prototype object:
p1.greet = function() {
console.log('Hello');
console.log(p1.greet());
Because the p1 object has the greet() method, JavaScript just executes it immediately without
looking it up in the prototype chain.
https://www.javascripttutorial.net/javascript-prototype/ 11/12
29/07/2022, 08:09 JavaScript Prototype
This is an example of shadowing. The greet() method of the p1 object shadows the greet()
method of the prototype object which the p1 object references.
Summary
The Object.prototype object has all properties and methods which are available in all objects
such as toString() and valueOf() .
The Object.prototype object has the constructor property that references the Object
function.
Every function has a prototype object. This prototype object references the
Object.prototype object via [[prototype]] linkage or __proto__ property.
The prototype chain allows one object to use the methods and properties of its prototype
objects via the [[prototype]] linkages.
The Object.getPrototypeOf() method returns the prototype object of a given object. Do use
the Object.getPrototypeOf() method instead of __proto__ .
https://www.javascripttutorial.net/javascript-prototype/ 12/12