Could you explain to me what is the difference between that two approaches (FirstModule and SecondModule:
var MyModule = (function () {
return {
method: function () {
console.log("MyModule:method");
}
}
})();
var FirstModule = (function () {
return {
test: function () {
MyModule.method();
}
}
})();
var SecondModule = (function (myMethod) {
return {
test: function () {
myMethod.method();
}
}
})(MyModule);
In both cases we have the same object, it isn't a copy. I would be grateful for any good information, advantages, disadvantages of any approach.
The first approach uses the
MyModulevariable, which can be overwritten by other code, outside the module, after theFirstModulevalue has been asigned.The second uses the
myMethodvariable, which can't (although properties of the object assigned tomyMethodstill can because that object is still globally available via theMyModulevariable).