To put it simply, is there any difference between these two pieces of code, and is there any reason to use one versus the other?
Code #1:
var thing=function(){}
thing.prototype={
some_method:function(){
alert('stuff');
}
}
Code #2:
var thing=function(){}
thing.prototype.some_method=function(){
alert('stuff');
}
That’s just the same. There is no reason why there would be a difference.
But in my opinion it’s better to use the second form as you have less to change if you decide to make the
thing“class” inherit from another one :It also make it easier to define your class in more than one javascript file.
And as it’s better to keep your code coherent, I’d prefer to use always the same construct, that is the
thing.prototype.some_method=function(){one.