-1

Possible Duplicate:
Explain JavaScript's encapsulated anonymous function syntax

I have just read a javascript book but I have seen this code:

1(function() {

          // code

})();

what is this ? is a special function ?

Community
  • 1
  • 1
xRobot
  • 25,579
  • 69
  • 184
  • 304

2 Answers2

1

It looks like the intent was to declare the function inline/anonymous and immediately execute it.

James
  • 12,636
  • 12
  • 67
  • 104
1

As written, it has a syntax error.

I'm guessing it was more like:

(function() {
          // code
})();

or

(function() {
          // code
    }
)();

Break it down:

(FOO)() // calls FOO with no arguments.

And

function() { //creates a function that takes no arguments.
      // code
}

Hence together it would create a function that takes no arguments, and then call it. I can't see why you would apart from just showing that you can.

Jon Hanna
  • 110,372
  • 10
  • 146
  • 251
  • You do so in JavaScript to create a protected scope. `var` s defined in that block will not be accessible in the outer scope. – gnarf Oct 07 '10 at 22:44