Which one is better?

1.Closure

function Class(){
   this.foo = functoin(){

   }
}

2.Prototype

function Class() {}
Class.prototype.foo = function() {

}

My answer is #2

  1. All method/object under prototype is shared among all instances. Memory free.
  2. Prototype is faster than closure when instanciating http://stackoverflow.com/questions/3493252/javascript-prototype-operator-performance-saves-memory-but-is-it-faster/4041582#4041582
  3. Closure is good for having private members, but private means not white-test-able.

Comments

comments powered by Disqus