1

file.js:

(function(m) {
    //some
    //code
    //here
}(m))

I can't get what does this construction mean?

sector119
  • 828
  • 8
  • 12

1 Answers1

3

That code defines a function inline and it immediately executes it.

Think about it by substituting the function for a variable:

(function(m) {
    //some
    //code
    //here
}(m))

Then that's the same as:

var f = function(m) {
    //some
    //code
    //here
}
(f(x))

But without having to define the f.

ssice
  • 3,215
  • 1
  • 24
  • 37
  • But why they use () surround function call? – sector119 Jun 19 '15 at 07:56
  • 2
    @sector119: Because without them, the parser is in a state where seeing the keyword `function` will start a function *declaration*. To invoke it immediately, we need the parser to start a function *expression*, so we use parens (or a `+`, or a `!`, etc.) to put the parser in a state where only an expression, not a declaration, is valid. [More in this answer.](http://stackoverflow.com/a/13341710/157247) – T.J. Crowder Jun 19 '15 at 07:57
  • 1
    The extra `()` makes it an expression rather than just a function definition which is required for it to work as desired. – jfriend00 Jun 19 '15 at 07:57