Why defining properties in the prototype is considered an antipattern

后端 未结 6 970
失恋的感觉
失恋的感觉 2021-01-02 18:59

I often see this pattern to define javascript objects

function Person(name) {
    this.name = name;
}
Person.prototype.describe = function () {
    return \"         


        
6条回答
  •  失恋的感觉
    2021-01-02 19:33

    Perhaps related: In general modifying an object that you don't own is considered an anti pattern.

    Meaning, if you didn't create the object then you don't "own" that object. Including:

    • Native objects (Object, Array, etc)
    • DOM objects
    • Browser Object Model (BOM) objects (such as window)
    • Library objects

    Source Maintainable Javascript by Nicholas C. Zakas

提交回复
热议问题