Thoughts about __proto__

What __proto__ is, where it is hiding, what does it do.

var obj = new Object([value]);

Object() is a constructor. It creates an object of a type corresponding to the given value. If value is omitted or null or undefined it returns empty object.

When a function object is created, it is given a prototype member which is an object containing a constructor member which is a reference to the function object.

Object() has following properties and methods:

  • length: 1
  • name: “Object”
  • prototype: Object
  • assign: assign(); //experimental
  • create: create();
  • defineProperties: defineProperties();
  • defineProperty: defineProperty();
  • freeze: freeze();
  • getOwnPropertyDescriptor: getOwnPropertyDescriptor();
  • getOwnPropertyNames: getOwnPropertyNames();
  • getOwnPropertySymbols: getOwnPropertySymbols(); //experimental
  • getPropertyOf: getPropertyOf();
  • is: is(); //experimental
  • isExtensible: isExtensible();
  • isFrozen: isFrozen();
  • keys: keys();
  • observe: obeserve(); //experimental
  • preventExtensions: preventExtensions()
  • seal: seal();
  • setPrototypeOf: setPrototypeOf() //experimental
  • __proto__ : function()

Object.prototype

Object.prototype is an instance of an Object. When the object is created through calling new Object() all properties and methods of Object.prototype are submitted to this new object. These inherited properties and methods may be overridden.

Object.prototype has following methods:

  • __defineGetter__:__defineGetter__(); //not standardized, deprecated
  • __defineSetter__: __defineSetter__(); //not standardized, deprecated
  • __lookupGetter__: __lookupGetter__(); //not standardized, deprecated
  • __lookupSetter__: __lookupSetter__(); //not standardized, deprecated
  • constructor: Object()
  • hasOwnProperty: hasOwnProperty();
  • isPrototypeOf: isPrototypeOf();
  • propertyIsEnumerable: propertyIsEnumerable()
  • toLocaleString: toLocaleString();
  • toSource: toSource(); //not standardized
  • toString: toString();
  • unwatch: unwatch(); //not standardized
  • valueOf: valueOf();
  • watch: watch(); //not standardized

__proto__

Object.__proto__ is a function with the native properties and methods.
Object.__proto__ is a prototype for constructor of Function.prototype.

Object._proto_ === Function.prototype; //true

It returns true because Object() is a function and inherits properties from Function.prototype.

Object.__proto__:

  • apply: apply();
  • arguments: null;
  • bind: bind();
  • call: call();
  • caller: null;
  • constructor: Function();
  • isGenerator: isGenerator();
  • length: 0;
  • name: “”,
  • toSource: toSource();
  • toString: toString();
  • __proto__: Object
Object.prototype.__proto__ === null //ture

Object.prototype.__proto__ is null because Object is a base object in JavaScript and does not inherit any properties form another object. So __proto_ in this case is empty and can’t be a prototype of the constructor of Object.prototype

Function.__proto__.__proto__ === Object.prototype //true
Function.__proto__ === Function.prototype //true

Function.__proto__ is prototype of a constructor of the Function.prototype object.
Function.__proto__ has __proto__ object which inherits set of properties from Object.prototype.
Reminder! Functions are objects which inherit a set of properties from Object.prototype.

Let’s create new class Wolf.

The constructor:

function Wolf(head, tail, paw) {
    this.head = head;
    this.tail = tail;
    this.paw = paw;
}

The constructor Wolf() is a function with:

  • arguments: null
  • caller: null
  • length:
  • name: “Wolf”
  • own prototype: Object {
    – properties of Wolf Class
    – methods of Wolf Class
    – constructor: Wolf();
    – __proto__: Object => object which contains native methods and properties of Object.prototype
    }
  • __proto__: function()
Wolf.__proto__ = Function.prototype; // true

The constructor Wolf() is a function, meaning it’s also an object  which inherits methods and properties form Function.prototype, and Function.prototype inherits methods and properties form Object.prototype so:

Wolf.prototype.__proto__ === Object.prototype; //true

Let’s create the new object of Wolf class:

var bigBadWolf = new Wolf('grey', 'long', 4);

The bigBadWolf is an object with following properties and methods:

  • head: ‘grey’,
  • paw: 4,
  • tail: ‘long’
  • __proto__ : Object {
    – methods and properties of Wolfe Class;
    – constructor: Wolf();
    – __proto__: Object
    }

The bigBadWolf.__proto__ is equal to Wolf.prototype.

bigBadWolf.__proto__ === Wolf.prototype // ture

The bigBadWolf.__proto__ contains __proto__ object which points to Object.prototype from which bigBadWof inherits its methods and properties:

bigBadWolf.__proto__.__proto__ === Object.prototype //true
So what is __proto__?

__proto__ sets the prototype of Object.
__proto__ points to the object which was used as prototype when the object was instantiated.
__proto__ is a property available in all instance.
__proto__ is the prototype of the constructor of __proto__’s object.