what is the difference below:
function test(){
this.init=function(){//do something}
this.some=function(){//do something}
function other(){}
}
and
function test(){
function init(){//do something}
function some(){//do something}
function other(){}
return {'init':init, 'some':some};
}
Thanks for explanation.
As Mike has already pointed out, there is a difference on how they need to be invoked (the first version requires
newwhile the second doesn’t).That said, if these are being used for actual modules (instead of just objects) and the functions in the module call each other then there is a difference.
In the second case the functions can reference each other directly (static binding).
But in the first case they would reference each other via dynamic binding through the
this:This means that in the first case (with the
this) the init function must be always called asmodule.init()and cannot be passed to a callbackBecause I don’t like having to retype the function names as in the second version, I personally prefer to use the following style in my modules: