I some times create class like this
function class1(){
.....
class1.prototype.callme = function(){
alert("hai");
}
}
then I instantiate using (new class1()).callme();
and some times I use modular pattern
var class2 = (function(){
var privatemethod = function(){
....
}
var publicmethod = function(){
alert("am public");
}
return{
callme:publicmethod
}
})();
then I call class2.callme()
Whats the advantage and disadvantage, can some body please explain.
prototypeinside the constructor function? In your first exampleclass1.prototype.callmeis overwritten every time you create a newclass1instance, which is kind of inefficient. And if you actually use(new class1()).callme()(i.e., that's not a simplification for this question) the whole class thing is kind of redundant.class1then that serves a different purpose to the second example but it's kind of hard to compare the two when you don't give a more realistic example.