__proto__ VS. prototype in JavaScript

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

    What about using __proto__ for static methods?

    function Foo(name){
      this.name = name
      Foo.__proto__.collection.push(this)
      Foo.__proto__.count++
    
    }
    
    Foo.__proto__.count=0
    Foo.__proto__.collection=[]
    
    var bar = new Foo('bar')
    var baz = new Foo('baz')
    
    Foo.count;//2
    Foo.collection // [{...}, {...}]
    bar.count // undefined
    

提交回复
热议问题