__proto__ VS. prototype in JavaScript

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

    To put it simply:

    > var a = 1
    undefined
    > a.__proto__
    [Number: 0]
    > Number.prototype
    [Number: 0]
    > Number.prototype === a.__proto__
    true
    

    This allows you to attach properties to X.prototype AFTER objects of type X has been instantiated, and they will still get access to those new properties through the __proto__ reference which the Javascript-engine uses to walk up the prototype chain.

提交回复
热议问题