__proto__ VS. prototype in JavaScript

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

    Explanatory example:

    function Dog(){}
    Dog.prototype.bark = "woof"
    
    let myPuppie = new Dog()
    

    now, myPupppie has __proto__ property which points to Dog.prototype.

    > myPuppie.__proto__
    >> {bark: "woof", constructor: ƒ}
    

    but myPuppie does NOT have a prototype property.

    > myPuppie.prototype
    >> undefined
    

    So, __proto__ of mypuppie is the reference to the .prototype property of constructor function that was used to instantiate this object (and the current myPuppie object has "delegates to" relationship to this __proto__ object), while .prototype property of myPuppie is simply absent (since we did not set it).

    Good explanation by MPJ here: proto vs prototype - Object Creation in JavaScript

提交回复
热议问题