среда, 21 декабря 2016 г.

__proto__ vs prototype

__proto__ vs prototype!


__proto__

  • Each object has a property '__proto__'
  • __proto__ is a private system property
  • When accessing the property of an object, it is first searched in this object:
    if false: it searched in __proto__:
        if false: it searched in __proto__.__proto__ and so on
  • __proto__ of every value (but not null and underfined) refers to prototype of it's data type:
  • 
    (0).__proto__ === Number.prototype &&
    false.__proto__ === Boolean.prototype &&
    "string".__proto__ === String.prototype &&
    (new Date).__proto__ === Date.prototype &&
    (function(){}/* new Function */).__proto__ === Function.prototype
    
    All data types inherits from Object:
    
    Number.prototype.__proto__ === Object.prototype
    
    But only Object has no prototype:
    
    Object.prototype.__proto__ === null
    

prototype

  • This is a common property except for two nuances:
    • Only functions in JavaScript have a prototype property. It defaults to the object with a single property constructor, which refers to the function itself.
    • Prototype property is used to create new objects using operator 'new'.

* this article is partial translate of Original article