What does this piece of JavaScript code mean?

I have not seen this type of grammar before. What does it mean? What technique does this apply to?

(function(fun) { })(myFunkyAlert); 
+3
javascript function grammar
source share
3 answers

This is an anonymous function that will run immediately after the announcement. Its parameter is myFunkyAlert , and inside the function it will be referenced as a fun variable.

The reason we usually write such a function is to avoid conflicts due to visibility.

Example:

 var myFunkyAlert = "The funky alert"; (function(fun) { alert(fun); })(myFunkyAlert); 

This will result in a warning with the message "Frightened warning."

+7
source share

You define an anonymous function and then call it with myFunkyAlert as an argument.

+2
source share

Check out this question: What is the difference between a vs declaration function expression in JavaScript? and this link: http://kangax.github.com/nfe/

0
source share

All Articles