1

Possible Duplicate:
What does this mean? (function (x,y)){…}){a,b); in JavaScript

Please anyone explain and give example about the Javascript below, I can't find any document that explain about this:

(function([arg, arg ...]) {

    //javascript code here

})([str, str ...]);

I'm asking this question because I see in the Google also Facebook javascript code, most of the them using this syntax.

2

4 Answers 4

4

It is anonymous function invocation

the arguments values str, str are passed to the function as arguments arg, arg and then the javascript code is executed.

it is the same as

function foo([arg, arg ...]) {
    //javascript code here
}

foo([str, str, ...]);
Sign up to request clarification or add additional context in comments.

1 Comment

+1 for getting it mostly right. :-)
3

What's happening is, an anonymous function expression is defined (in parentheses) -

(function([arg, arg ...]) {

    //javascript code here

})

The function is then immediately called using the () at the end of the function, just as as you'd call a normal function var result = someFunction(). So , for example -

(function() {
    alert('Called by "()" at end of function');
})()

You can also pass arguments to the anoymous function, via the parentheses -

(function(username) {
    alert('Called by "()" at end of function. Hello ' + username + '!');
})('Dave')

2 Comments

+1 - speed is the enemy of accuracy. :-)
@RobG - Thanks! Not sure I can blame it all on speed though :)
1
(function(a, b) {
  var x;
})

returns an anonymous function. For technical/historical reasons, anonymous functions must be wrapped in parentheses.

Notably, the names a,b and x are not visible to outside code. In contrast, just writing var x; would make x accessible and thereby pollute the global namespace. This is the equivalent of private variables in classical object-orientated languages.

We could also write:

var func = function(a, b) {var x;};
// or equivalently:
function foo(a, b) {var x;};

func('arg1', 2);

but that would again pollute the global namespace by making a function func visible for everyone else. Since we don't want to call func more than once anyway,

(function(a, b) {var x;}) ('arg1', 2);

is the perfect way to execute code with variables without making these variables(or any variable names) visible to the rest of the world.

2 Comments

The expression at the top of your post is a function declaration with no name, so it is a syntax error. Given a name to make it syntactically legal, it has no return statement so returns undefined, not a function.
@RobG Thanks. Fixed: Enclosed with parentheses to make it syntactically legal, it indeed returns a function. Note that anonymous functions do not need to be wrapped in parentheses in expressions, only in statements.
1

The function in round brackets is called just once with args defined by strs.

The round brackets containing the function are used to limit the scope of the function, i.e. the function cannot be called externally.

The str variables can be global variables (e.g. jQuery) and can be aliased by their corresponding arg (e.g. jQuery -> $) for the sole use of the function.

Generally, this is a good, safe way to write javascript, e.g.

(function($) {
   // code
})(jQuery)

For the function, the local variable '$' means the global variable 'jQuery'. The means you can have '$' defined to be something else in another script (or function) and that variable will not affect '$' in this function.

2 Comments

The round brackets containing the function are not scope operators. They are there to allow the function to be invoked immediately. If they weren't there, a syntax error would occur.
Ah, good point - thanks. Although, my point that the function cannot be called externally (since it's anonymous) is still true.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.