It creates a function and calls it immediately, passing in window
. The function receives an argument called window
and then creates an empty object on it which is available both as a property on window
called test
and as a local variable called test
. Then it creates an object by calling a function via new
and assigns that object to test.utils
.
I don't understand what the purpose of the last part is (window);...
It doesn't really serve any purpose in the code you've quoted, because the symbol passed into the main (outer) function, window
, is the same as the name of the argument receiving it. If their names were different, then it would serve a purpose, e.g.:
(function(wnd) {
})(window);
That would make window
available within the function as wnd
.
or why the utils function is being designated as new.
utils
won't be a function (at least, not unless the code you've replaced with ...
is doing something really strange), it will be an object created by calling that function.
The whole thing could be rewritten more clearly:
(function(window) {
var test;
test = {};
window['test'] = test;
test.utils = new NiftyThing();
function NiftyThing() {
}
})(window);
That still does the window
thing for no reason, but hopefully it makes it clear what the new(function() { ... })();
bit was doing.