Are there any benefits from prototype methods except it’s context and global availability across all instances?
Creating new instance of object without prototyped methods eats more memory than vice versa?
Just can’t understand, why some developers are storing initial methods inside prototype when shared context is not required.
The only thing i’m thinking about is memory usage…
// first one
function t(){
this.method1=function(){
};
this.method2=function(){
};
}
// second one
function tt(){
}
tt.prototype={
method1:function(){
}
,method2:function(){
}
}
var storage1=[];
var storage2=[];
var i=0;
while(i<10000) {
storage1.push(new t());
storage2.push(new tt());
i++;
}
Yes, it eats tons of memory and the memory eating scales with how complex the “class” is and how many instances you create. This is a very good reason to use prototyped methods and greatly outweighs the benefits from “private methods”.
I don’t know what you mean by “when shared context is not required”.