Example:
<script>
var a = 1;
var b = 2;
(function () {
var b = 3;
A + b;
}) ();
alert (a); 4
alert (b);//2
</script>
Indicates that the anonymous function has been executed--> this is the self execution of the anonymous function
Self-executed anonymous function
1. What is a self executing anonymous function?
It refers to a function such as this: (function {//code});
2. Why (function {//code}); can be executed, and function {//code} ();
(1). First, be clear about the difference between the two:
(function {//code}) is an expression, function {//code} is a declaration of functions.
(2). Second, JS "precompiled" features:
JS in the "precompiled" phase, will interpret the function declaration, but will be a slight.
(3). When JS executes to function () {//code} (), because function () {//code} has been interpreted in the precompiled phase, JS skips function () {//code} to attempt to execute ();
When JS executes to (function {//code}), because (function {//code}) is an expression, JS will be able to solve it to get the return value, because the return value is a function, and therefore encountered (), it will be executed.
In addition, the method of converting a function to an expression does not necessarily depend on the grouping operator (), we can also use the void operator, the ~ operator ...
Reference: http://www.jcodecraeer.com/a/jquery_js_ajaxjishu/2012/0628/290.html
http://bonsaiden.github.io/JavaScript-Garden/zh/#function. This
http://mianshiti.diandian.com/post/2013-04-05/40048538140