Why defining properties in the prototype is considered an antipattern

Viewed 1960

I often see this pattern to define javascript objects

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

And in this article it says that adding properties directly to the prototype objct is considered an anti-pattern.

Coming from "classical class based" languages, having to define the properties apart from methods doesn't sound quite right, moreoever in javascript, where a method should be just a property with a function value (am I right here?)

I wanted to know if anybody can explain this, or even suggest a better way to handle these situations

6 Answers

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

Related