0

Below are some code

var Foo=function(){
   this.doSomething1=function(){ 
      //do something

   }

};
Foo.prototype.doSomething2=function(){

     //do something
};

if I change it to

var Foo=function(){


};
Foo.prototype.doSomething1=function(){

  //do something
};
Foo.prototype.doSomething2=function(){

  //do something
};

the second block of code almost can do any thing same as the first one. but it has very clear inherit relationship on prototype chain.

What is the advantage for the first code block?

Your comment welcome

arachide
  • 8,006
  • 18
  • 71
  • 134
  • In first approach, `doSomething1` is a part of every object and if you try `Object.hasOwnProperty` this will return true. Prototype are more for generic functions that are shared across objects – Rajesh Sep 21 '16 at 06:08
  • Disadvantage: doSomething1 will be stamped out for every instance and consuming memory. doSomething2 exists only once. – hgoebl Sep 21 '16 at 06:09

0 Answers0