3
var myval = (function(){})();

I don't understand (function..) meaning and even else code.

aaronasterling
  • 68,820
  • 20
  • 127
  • 125
Ickhyun Kwon
  • 1,675
  • 4
  • 13
  • 17
  • It's the syntax for an anonymous function in javascript – Phil C Feb 24 '11 at 07:31
  • possible duplicate of [What's the role of the parentheses in the following piece of code?](http://stackoverflow.com/questions/2938060/whats-the-role-of-the-parentheses-in-the-following-piece-of-code) – Greg Hewgill Feb 24 '11 at 07:42

6 Answers6

8

What you've got there is a:

self-invoking anonymous function

You're first creating a function-expression by having paranthesis around the function itself. Just to write

function() {
}()

would not work in this instance, because this would define a function-declaration.

So after we have that, we can call itself by appending ()

(function() {
})();

To verify that, try this:

var myval = (function(){return 'self executed!'})();

alert(myval); // === 'self executed'
jAndy
  • 231,737
  • 57
  • 305
  • 359
8
  • function(){} — is a function expression, it defines a function
  • (function(){}) — wrapping it like this makes sure it gets treated as an expression
  • (function(){})() — Adding () calls the function

And then the return value is assigned to a variable.

This is usually used to allow variables to be used without polluting the global scope.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • 1
    `function(){}` will only be an expression in an assignment. – jAndy Feb 24 '11 at 07:37
  • 1
    @jAndy, not only in an assignment, there are other contexts of *expression*, for example: `0,function(){}`, `new function(){}`, `foo(function(){})`, etc... The restriction on the grammar is at the level of the *ExpressionStatement*, it cannot start with either `{` or the token `function`. On *statement context* `function(){}` will be just a `SyntaxError` (no production of the grammar matches it). – Christian C. Salvadó Feb 24 '11 at 08:21
  • @CMS: yes, I just wanted to clarfiy that just `function(){}` is not necessarily an expression. `+function(){}()` or `!function(){}()` will work also :-) – jAndy Feb 24 '11 at 08:34
2

This creates an anonymous function and immediately calls it. For example

(function ($) {
  // Original JavaScript code.
})(jQuery);

will allow you to use $ in there and it equals jQuery.

chx
  • 11,270
  • 7
  • 55
  • 129
  • +1 for common example -- I always start it as `;(function ...` though (because I write semi-colon free code and this is one case where ASI might not work as expected) –  Feb 24 '11 at 09:19
1

This function(){} defines anonymous function (closure) with no body. By wrapping it in braces and adding empty parameters list at the end (()) you are running this closure. This is essentially equivalent to:

var f = function() {};
f();

Would this be easier to grasp?

(function(x, y, z){})(1, 2, 3)
Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
  • Corrected, thx. Just to clarify - would it be enough to call this a closure if the function would use some local variables defined outside? – Tomasz Nurkiewicz Feb 24 '11 at 08:03
  • It's getting closer. It has to make reference to a local variable from an enclosing scope when executed _outside_ of that scope. Practically, this means that it has to be returned from a function and then executed in addition to it making reference to a local from an enclosing scope for it to be regarded as a closure. – aaronasterling Feb 24 '11 at 08:12
0

Let's analyze it piece by piece:

This define an anonymous function (i.e. a function with no name)

function(){}

Of course it would be more useful to put some instruction in between the {} brackets.

Now if you did

myval = function(){<something>};

You assign the function to myval (the function, NOT its return value!)

So then you could call myval() and it would be the same as calling the function itself.

Here instead you do call the function by putting () at the end. Therefore:

var myval = (function(){})();

calls the function, and puts the result (not the function itself this time) in myval

nico
  • 50,859
  • 17
  • 87
  • 112