What's the difference between this.bla to Object.prototype.bla

后端 未结 4 1410
既然无缘
既然无缘 2021-01-20 11:30

Let\'s say I have this code:

(function(global) {
    function Bar(foo) {
        this.foo = foo;
        return this;
    }

    Bar.prototype.getFoo = funct         


        
4条回答
  •  渐次进展
    2021-01-20 12:17

    An aside :

    With prototypal inheritance, there's a fundamentel difference between own properties and inherited ones.

    Sometimes this can be quite relevant.

    A classic for loop that is often used, only checks for the 'own properties', taking this form :

    for (prop in array) {
    if (array.hasOwnProperty(prop)) {
          dostuff
    }
    

    When assigning to this, alle properties are own properties, making the hasOwnProperty check irrelevant.

提交回复
热议问题