[removed] Overwriting function's prototype - bad practice?

后端 未结 6 546
天涯浪人
天涯浪人 2020-12-13 00:51

Since when we declare a function we get its prototype\'s constructor property point to the function itself, is it a bad practice to overwrite function\'s prototype like so:<

6条回答
  •  我在风中等你
    2020-12-13 01:16

    This form:

    LolCat.prototype = {
      hello: function () {
          alert('meow!');
      }
    };
    

    Destroys any existing methods and public properties. In the example, as given, it doesn't matter as the newly created LolCat doesn't have any properties or methods. However, one should be mindful of this in more complicated code.

    This form:

    LolCat.prototype.hello = function () { ... }
    

    Adds a new method to an existing object and keeps the existing intact.

提交回复
热议问题