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

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

3

6 Answers 6

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'
Sign up to request clarification or add additional context in comments.

Comments

7
  • 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.

3 Comments

function(){} will only be an expression in an assignment.
@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).
@CMS: yes, I just wanted to clarfiy that just function(){} is not necessarily an expression. +function(){}() or !function(){}() will work also :-)
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.

1 Comment

+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)
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)

2 Comments

Corrected, thx. Just to clarify - would it be enough to call this a closure if the function would use some local variables defined outside?
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.
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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.