I just started working in a new company and noticed something completely wrong for me in many of my JSs. I am a little hesitant to pick it up without confirming that it is wrong, since I am quite young, I am not a JS expert, and this is only my second day and I do not want to look stupid.
So, usually I expect the module template to look something like this:
MODULENAME = MODULENAME || {}; MODULENAME.SUBMODULENAME = (function() { var bla = {}; bla.somefunction = function() {
What do they have in all of their code:
MODULENAME = MODULENAME || {}; MODULENAME.SUBMODULENAME = (function() { var that = this; that.somefunction = function() {
Now, of course, because the function is not called as a constructor with the new
keyword or as a method, this
bound to window
, and they define that
as this
. Thus, they basically flush everything in the global object, and all of their submodule names are actually aliases for window
. Is there a reason someone would want to do this? Or is it really as wrong as it seems to me?
Edit:
I was mistaken in putting var
before the definition of the submodule, initially I wrote something a little different and forgot to delete var
. I tried to make the example even clearer, I hope it will be more obvious what I mean now.
Edit 2:
I also looked at the scripts executed in Firebug, and they definitely add everything to the window
, this object is a complete mess.
source share