JS Immediate execution function allows you to execute the function immediately after creation, JS immediately execute function pattern is a syntax, you can make your function immediately after the definition is executed, this pattern is essentially a function expression (named or anonymous), immediately after the creation of the execution.
First, JS immediately execute the function of the wording
Way 1, the last parenthesis
Mode 2, function outside parentheses
Mode 3, function front plus operator, common is! with void
Second, immediately execute the parameters of the function
Arguments can be passed to an immediate execution function, such as
(function (who, when) { console.log ("I met" + ' who + ' on ' + when);} ("Joe Black", New Date ()));
Remember:
1, immediately inside the function can access external variables, so in many cases, we do not need to pass parameters. such as: jquery window arguments, if not passed in. The interior is also available for direct use.
2. Usually you should not pass too many functions to the immediate execution function, because it will soon become a burden--in order to understand how the code works, you have to scroll the source code up and down frequently.
JS immediately executes function usage