What's the recommended way to declare fields of a class in JavaScript?

Posted by Bytecode Ninja on Stack Overflow See other posts from Stack Overflow or by Bytecode Ninja
Published on 2010-06-14T02:35:50Z Indexed on 2010/06/14 2:42 UTC
Read the original article Hit count: 224

Is it better to declare fields of a class in this way:

function MyClass() {
   this.aMethod = function() {
     // code
   }
}

Or this way:

function MyClass() {
}

MyClass.prototype.aMethod = function() {
  // code
}

Any why?

Thanks in advance.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about object-orientation