__proto__ VS. prototype in JavaScript
__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
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.