prototype and __proto__

Every object in javascript has a __proto__ property. The value of __proto__ is the prototype property of its construction function, or null if the object is not constructed by a construction function(such as Object.prototype.__proto__). Because everything in javascript is an object including function, the function’s __proto__ property is an empty function “function ()” because it has no construction function. Let’s see the following example:

<!DOCTYPE html>
<html>
  <head>
    <meta charset=utf-8 />
  </head>
  <body>
    <script>
    function Person()
    {

    }
    var person=new Person();
    console.log("person.__proto__:",person.__proto__);
    console.log("Person.prototype:",Person.prototype);
    console.log("person.__proto__===Person.prototype:",person.__proto__===Person.prototype);
    console.log("Person.__proto__:",Person.__proto__);
    console.log("person.prototype:",person.prototype);
    console.log("Person.prototype.prototype:",Person.prototype.prototype);
    console.log("Person.prototype.__proto__:",Person.prototype.__proto__);
    console.log("Object.prototype:",Object.prototype);
    console.log("Person.prototype.__proto__===Object.prototype:",Person.prototype.__proto__===Object.prototype);
    console.log("Object.prototype.__proto__:",Object.prototype.__proto__);
    console.log("Object.prototype.prototype:",Object.prototype.prototype);
    console.log("Object.__proto__:",Object.__proto__);
    console.log("Number(1).__proto__:",Number(1).__proto__);
    console.log("Number.prototype:",Number.prototype);
    </script>
  </body>
</html>

The output is:

 

When searching for a property in an object and the object itself has not the property, it will look up the property in the __proto__ property of the object. This lookup process continues until the __proto__ is null(in the Objet.prototype). If the property is still not found, it is an “undefined” property. In the above example, person.prototype is an undefined property. A function’s “prototype” property always exists, and its constructor property is the function itself.

reference

Comments are closed, but trackbacks and pingbacks are open.