DEV Community

Cover image for JavaScript __proto__ and prototype

Posted on

JavaScript __proto__ and prototype

There are many explanation of proto and prototype, Have a look on what i have covered

prototype is a property of a Function object. It is the prototype of objects constructed by that function.

proto is internal property of an object, pointing to its prototype. Current standards provide an equivalent Object.getPrototypeOf(O) method, though 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;
Enter fullscreen mode Exit fullscreen mode

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.



Top comments (0)