3

Can someone explain in detail what this snippet of js does?

(function (window) { var test = window['test'] = {}; test.utils = new(function(){ ... })(); })(window); 

I understand that the function is not globally scoped. I understand that it is creating a variable called test that points to a property in the window object that is an empty object. I also understand that utils is a property of test.

I don't understand what the purpose of the last part is (window); or why the utils function is being designated as new.

Please explain.

7
  • 1
    It's a slightly misguided attempt to make the code in the anonymous (outer) function "safe" from tampering with window. It's misguided because it fails to do that :-) Instead of passing window, the code should pass this.
    – Pointy
    CommentedFeb 7, 2013 at 14:56
  • Neither do I understand. Where did you get that code from, and what does the constructor-invoked function expression contain?
    – Bergi
    CommentedFeb 7, 2013 at 14:57
  • possible duplicate of What does this mean? (function (x,y)){...}){a,b); in JavaScriptCommentedFeb 7, 2013 at 14:58
  • 2
    new(function(){ ... })(); is just defining the constructor function and creating an instance in one go. It's the same as function Foo() {...} and new Foo();. Using an object literal might be easier here, but I cannot tell since I don't know what's the content of the function.CommentedFeb 7, 2013 at 15:00
  • possible duplicate of JS function definition : meaning of the last parentheses
    – sdespont
    CommentedFeb 7, 2013 at 15:01

4 Answers 4

2

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.

3
  • If you use the window variable, it might help minification. That's the only reason I've heard of, yet I think such things should be done by the minifier :-)
    – Bergi
    CommentedFeb 7, 2013 at 15:33
  • so what would be the difference between test.utils = new(function(){ ... })(); and test.utils = function(){ ... };?
    – bflemi3
    CommentedFeb 7, 2013 at 17:17
  • @bflemi3: In the first, test.utils will refer to an object that almost certainly isn't a function. In the second, test.utils will refer to a function.CommentedFeb 7, 2013 at 17:43
1

First of all, this is a self-invoked function.

It's invoked itself giving the window object as function input argument in order to ensure that, inside the whole function, window will have the expected meaning.

test.utils = new(function(){ ... })(); <--- This is an object constructor. 

When the function is called using the new operator, it turns into an object constructor.

For example:

var conztructor = function() { this.name = "Matias"; }; var obj = new conztructor(); alert(obj.name); // <--- This will alert "Matias"! 

The purpose of (window); is creating a new variable and reference holding the JavaScript Window object instance, avoiding that other libraries may reuse the window (or any other) identifier and your own library may break because of this situation.

This is nice in order to avoid altering global scope identifiers that may be used by other libraries.

UPDATE

In response to some comments, run this code:

http://jsfiddle.net/wChh6/5/

18
  • Why do you think the window variable avoids modifications of the global object?
    – Bergi
    CommentedFeb 7, 2013 at 15:10
  • ^^^^^--- Its passed as reference, modifications will apply to the global object too, thats why i don't understand the purpose behind this jsbin.com/emaqog/1/editCommentedFeb 7, 2013 at 15:11
  • @Bergi Ok, maybe I explained it in the wrong way. I mean that you avoid overwriting the identifier and change what window or anything is designed for.CommentedFeb 7, 2013 at 15:12
  • @Glutamat Check my update, now I explain it better than before :)CommentedFeb 7, 2013 at 15:16
  • 1
    jsfiddle.net/wChh6/3 -> that doesn't change anything, you cant set window to null. And i don't really see any purpose, in setting the copied reference to the window Object to null, eitherCommentedFeb 7, 2013 at 15:25
0

What is happening here is that a new anonymous function is being declared. The last part with (window) makes a call to that function, passing window as a parameter.

Inside, the call to test.utils = new(function(){ ... })(); creates a new object (with contents defined by the function passed to new) and assigns it to test.utils.

    0

    When you define a function with last parentetheses, the function is executed itself after loading with the parameter given, in your case window

    (function (window) { var test = window['test'] = {}; test.utils = new(function(){ ... })(); })(window); 

    JS function definition : meaning of the last parentheses

      Start asking to get answers

      Find the answer to your question by asking.

      Ask question

      Explore related questions

      See similar questions with these tags.