A function is one of the most flexible objects in JavaScript, and it simply explains the purpose of its anonymous function. An anonymous function is a function that does not specify a function name or pointer, and the execution of an anonymous function is only one of them, which is called the function: self-execution function
The following is one of the most common self-execution functions:
Traditional anonymous functions
(function () {
alert (' Hello ');
}) ();
The result of this code is that it pops up when the page is loaded again: "Hello"
What prompted it to automate? , look at the following code
Remove the parentheses in the traditional notation and precede with the operator ~,!, +,-
~function () {
alert (' Hello ');
} ();
! function () {
alert (' Hello ');
} ();
+function () {
alert (' Hello ');
} ();
-function () {
alert (' Hello ');
} ();
There is no difference between these and the traditional ways of doing it,
I found that the common denominator of these notation is the operator, in fact, the traditional way of the parentheses () is also a kind of operation, appearing in: a=b* (c+d),
operator + passed to the native parameter = function auto execution? But some symbols also do not support, such as "=,*,/", the reason for its implementation is still very mysterious, the Internet can not find a decent answer
And then I discovered a magical phenomenon where these operators can stack infinitely ...
function is preceded by a specific symbol can be infinite superposition ...
~!+-~!+-+-!~!+-~!+-+-!~!+-~!+-+-!~!+-~!+-+-!~!+-~!+-+-!~!+-~!+-+-!~!+-~!+-+-!~!+-~!+-+-!~!+-~!+-+-!~!+-~!+-+-!~ !+-~!+-+-!~!+-~!+-+-!~!+-~!+-+-!~!+-~!+-+-!~!+-~!+-+-!~!+-~!+-+-!~~~~~~~~+-!!!!!! +function A (b) {
alert (' Hello ');
} ();
The program is running normally!!!!!!
However, there are two cases of error
If there are three or more consecutive "+" or "-" symbols, there will be an error;
If two consecutive "+" or "-" symbols are followed by other symbols, there will be an error, such as "++~", "--+";
Error tip: uncaught referenceerror:invalid left-hand side expression in prefix operation (meaning left expression error)
Then I use the same notation to compute a variable and find the exact same one, which may already indicate that the JavaScript operation prompts the function to execute automatically, or it can be understood to call this function by operation!
The function is not executed by itself, but by an operation. , but only partial operation mode is supported!
In addition, this self execution function is not necessarily an anonymous function! Looking at the top of the code, I defined the function name a in the function after the operator, and nothing unusual, but also no eggs-!, this a is still unable to be called by other methods, but I think a lot of people call it the anonymous function is a bit inappropriate!
The above is a small set to introduce the JavaScript from the implementation of the anonymous function (function () {}) of the principle of analysis, I hope to help you!