__proto__ VS. prototype in JavaScript

后端 未结 30 1904
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-21 06:14

This figure again shows that every object has a prototype. Constructor function Foo also has its own __proto__ which is Function.prototype, a

30条回答
  •  逝去的感伤
    2020-11-21 06:51

    Another good way to understand it:

    var foo = {}
    
    /* 
    foo.constructor is Object, so foo.constructor.prototype is actually 
    Object.prototype; Object.prototype in return is what foo.__proto__ links to. 
    */
    console.log(foo.constructor.prototype === foo.__proto__);
    // this proves what the above comment proclaims: Both statements evaluate to true.
    console.log(foo.__proto__ === Object.prototype);
    console.log(foo.constructor.prototype === Object.prototype);
    

    Only after IE11 __proto__ is supported. Before that version, such as IE9, you could use the constructor to get the __proto__.

提交回复
热议问题