__proto__ VS. prototype in JavaScript
This figure again shows that every object has a prototype. Constructor function Foo also has its own
__proto__
which is Function.prototype, and which in turn also references via its__proto__
property again to the Object.prototype. Thus, repeat, Foo.prototype is just an explicit property of Foo which refers to the prototype of b and c objects.
var b = new Foo(20);
var c = new Foo(30);
What are the differences between __proto__
and prototype
?
The figure was taken from dmitrysoshnikov.com.
Note: there is now a 2nd edition (2017) to the above 2010 article.
Solution 1:
__proto__
is the actual object that is used in the lookup chain to resolve methods, etc. prototype
is the object that is used to build __proto__
when you create an object with new
:
( new Foo ).__proto__ === Foo.prototype;
( new Foo ).prototype === undefined;
Solution 2:
prototype
is a property of a Function object. It is the prototype of objects constructed by that function.
__proto__
is an internal property of an object, pointing to its prototype. Current standards provide an equivalent Object.getPrototypeOf(obj)
method, though the de facto standard __proto__
is quicker.
You can find instanceof
relationships by comparing a function's prototype
to an object's __proto__
chain, and you can break these relationships by changing prototype
.
function Point(x, y) {
this.x = x;
this.y = y;
}
var myPoint = new Point();
// the following are all true
myPoint.__proto__ == Point.prototype
myPoint.__proto__.__proto__ == Object.prototype
myPoint instanceof Point;
myPoint instanceof Object;
Here Point
is a constructor function, it builds an object (data structure) procedurally. myPoint
is an object constructed by Point()
so Point.prototype
gets saved to myPoint.__proto__
at that time.
Solution 3:
prototype
property is created when a function is declared.
For instance:
function Person(dob){
this.dob = dob
};
Person.prototype
property is created internally once you declare above function.
Many properties can be added to the Person.prototype
which are shared by Person
instances created using new Person()
.
// adds a new method age to the Person.prototype Object.
Person.prototype.age = function(){return date-dob};
It is worth noting that Person.prototype
is an Object
literal by default (it can be changed as required).
Every instance created using new Person()
has a __proto__
property which points to the Person.prototype
. This is the chain that is used to traverse to find a property of a particular object.
var person1 = new Person(somedate);
var person2 = new Person(somedate);
creates 2 instances of Person
, these 2 objects can call age
method of Person.prototype
as person1.age
, person2.age
.
In the above picture from your question, you can see that Foo
is a Function Object
and therefore it has a __proto__
link to the Function.prototype
which in turn is an instance of Object
and has a __proto__
link to Object.prototype
. The proto link ends here with __proto__
in the Object.prototype
pointing to null
.
Any object can have access to all the properties in its proto chain as linked by __proto__
, thus forming the basis for prototypal inheritance.
__proto__
is not a standard way of accessing the prototype chain, the standard but similar approach is to use Object.getPrototypeOf(obj)
.
Below code for instanceof
operator gives a better understanding:
object instanceof
Class operator returns true
when an object is an instance of a Class, more specifically if Class.prototype
is found in the proto chain of that object then the object is an instance of that Class.
function instanceOf(Func){
var obj = this;
while(obj !== null){
if(Object.getPrototypeOf(obj) === Func.prototype)
return true;
obj = Object.getPrototypeOf(obj);
}
return false;
}
The above method can be called as: instanceOf.call(object, Class)
which return true if object is instance of Class.
Solution 4:
To explain let us create a function
function a (name) {
this.name = name;
}
When JavaScript executes this code, it adds prototype
property to a
, prototype
property is an object with two properties to it:
constructor
__proto__
So when we do
a.prototype
it returns
constructor: a // function definition
__proto__: Object
Now as you can see constructor
is nothing but the function a
itself
and __proto__
points to the root level Object
of JavaScript.
Let us see what happens when we use a
function with new
key word.
var b = new a ('JavaScript');
When JavaScript executes this code it does 4 things:
- It creates a new object, an empty object // {}
- It creates
__proto__
onb
and makes it point toa.prototype
sob.__proto__ === a.prototype
- It executes
a.prototype.constructor
(which is definition of functiona
) with the newly created object (created in step#1) as its context (this), hence thename
property passed as 'JavaScript' (which is added tothis
) gets added to newly created object. - It returns newly created object in (created in step#1) so var
b
gets assigned to newly created object.
Now if we add a.prototype.car = "BMW"
and do
b.car
, the output "BMW" appears.
this is because when JavaScript executed this code it searched for car
property on b
, it did not find then JavaScript used b.__proto__
(which was made to point to 'a.prototype' in step#2) and finds car
property so return "BMW".