1

Possible Duplicate:
What do parentheses surrounding a JavaScript object/function/class declaration mean?

I have found the following code in a website .

var testModule = (function(){

    var counter = 0;

    return {

       incrementCounter: function() {

            return counter++;

        },

        resetCounter: function() {

            console.log('counter value prior to reset:' + counter);

            counter = 0;

        }

    };

})();

So it follows the syntax var a = (blah balh..)()

What does it actually mean? What is the meaning of variable declaration like a =()()..

Community
  • 1
  • 1
Jinu Joseph Daniel
  • 5,864
  • 15
  • 60
  • 90
  • 1
    Note that it is not `(blah blah)()`, it's `(function (){ blah })()`. That is to say that this syntax only makes sense for function expressions. – nnnnnn Jan 23 '12 at 04:18

2 Answers2

5

It's defining a single-use function and executing it immediately. The code you provided is named the Module Pattern -- see here for more information about its properties: http://www.yuiblog.com/blog/2007/06/12/module-pattern/

A normal function might be created like this:

var f1 = function() {
  console.log('bar');
};

And you could subsequently call it like so:

f1();

But in the example you provided, the function is both defined and executed once, and that function returns an object with two functions: incrementCounter and resetCounter. You can call them like so: testModule.incrementCounter() and testModule.resetCounter()

The Module Pattern is useful when you have a single object and you want to encapsulate some properties which are only available to the functions defined in the closure.

ryanlahue
  • 1,151
  • 1
  • 6
  • 6
2

The anonymous function is executed and the return value is assigned to the variable.

James Montagne
  • 77,516
  • 14
  • 110
  • 130
  • 1
    Just curious, this can be considered as a closure versus `function(){}();` is not a closure and only an anonymous function? – Matt Lo Jan 23 '12 at 04:14
  • 1
    @Matt Lo: These are equivalent: function(){}() and (function(){})() The second form is recommended as a best practice. – ryanlahue Jan 23 '12 at 04:18
  • @rla: `function(){}()` is a `SyntaxError`. A function declaration requires a name. You need to remove the ambiguity between a declaration and an anonymous function with additional syntax. –  Jan 23 '12 at 04:21
  • @am not i am - They're equivalent _in an assignment_ or other place where they could only be interpreted as a function expression. On a line by itself the first is a syntax error because it is interpreted as an invalid function delcaration. – nnnnnn Jan 23 '12 at 04:23
  • @nnnnnn: Right, like I said you need additional syntax. Most operators (including the assignment operator) will force the function to be evaluated as part of an expression, removing the ambiguity. –  Jan 23 '12 at 04:27
  • 2
    @Matt Lo - the term ["closure"](http://en.wikipedia.org/wiki/Closure_(computer_science)) applies to certain behaviour in _any_ JavaScript function, whether anonymous or not. – nnnnnn Jan 23 '12 at 04:29
  • @nnnnnn I agree with your statement, that's how I've been using the term for JS for years, but been hearing controversy on many occasions (not just on SO) I've posted the question to see what votes more. – Matt Lo Jan 23 '12 at 04:36