3

I have not met this type of grammar before. What does it mean? To what technique is it related?

(function(fun) { 

})(myFunkyAlert);
Ates Goral
  • 137,716
  • 26
  • 137
  • 190
StevenWang
  • 3,625
  • 4
  • 30
  • 40

2 Answers2

7

This is an anonymous function that will run as soon as it is declared. Its parameter is myFunkyAlert and inside the function it will be referenced as the fun variable.

The reason we usually write a function like that is to avoid conflicts, due to scoping.

Example:

var myFunkyAlert = "The funky alert";

(function(fun) { 
   alert(fun);
})(myFunkyAlert);

This will result in an alert with the message "The funky alert".

Ates Goral
  • 137,716
  • 26
  • 137
  • 190
Ibu
  • 42,752
  • 13
  • 76
  • 103
2

You're defining an anonymous function and then calling it with myFunkyAlert as an argument.

Noufal Ibrahim
  • 71,383
  • 13
  • 135
  • 169