__proto__ VS. prototype in JavaScript

后端 未结 30 1915
爱一瞬间的悲伤
爱一瞬间的悲伤 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:36

    I'll try a 4th grade explanation:

    Things are very simple. A prototype is an example of how something should be built. So:

    • I'm a function and I build new objects similar to my prototype

    • I'm an object and I was built using my __proto__ as an example

    proof:

    function Foo() { }
    
    var bar = new Foo()
    
    // `bar` is constructed from how Foo knows to construct objects
    bar.__proto__ === Foo.prototype // => true
    
    // bar is an instance - it does not know how to create objects
    bar.prototype // => undefined
    

提交回复
热议问题