Functions are the most flexible object in JavaScript. Here we just explain the purpose of its anonymous functions. Anonymous functions refer to functions that do not specify function names or pointers. Self-executing anonymous functions are only one of them. The following is called this function: self-executing function.
Here is one of the most common self-executing functions:
// Traditional anonymous function(function() {alert('hello');})();The execution effect of this code is to pop up when the page is reloaded: "hello"
What prompts it to execute automatically? , see the following code
// Remove the brackets in traditional writing and add the operator ~,!,+,-~function(){alert('hello');}();!function(){alert('hello');}();+function(){alert('hello');}();-function(){alert('hello');}();These writing methods are no different from the traditional methods mentioned above.
I found that the common point of these writing methods is operators. In fact, the traditional way of brackets () also belong to a type of operation, which appears in: a=b*(c+d).
Operator + Parameters passed to self-generated = Functions are automatically executed? But some symbols are not supported either, such as the "=, *, /" sign. The reason why it executes itself is still very mysterious, and there is no decent answer on the Internet.
Then I discovered a magical phenomenon where these operators can be superimposed infinitely. . . . . .
// The function is preceded by a specific symbol that can be superimposed infinitely...~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~!+-~~~~~~~~~~~~~~~!!+!+-~~~~~~~~~~~~~~~!!+!+!+-~~~~~~~~~~~~~
The program runs normally!!!!!!
However, there are two situations where errors will be reported
If three or more "+" or "-" symbols appear in succession, an error will occur;
If two "+" or "-" symbols appear in succession and are followed by other symbols, an error will occur, such as "++~", "--+";
Error prompt: Uncaught ReferenceError: Invalid left-hand side expression in prefix operation (meaning the left expression error)
Then I used the same symbol to calculate a variable and found that it was exactly the same. This may already be explained that JavaScript's operation prompts the automatic execution of the function, or it can be understood as calling this function through operations!
It is not that the function executes itself, but that this function is called through operations! , but only some calculation methods are supported!
In addition, this self-executing function may not be an anonymous function! Looking at the code above, I defined the function name a in the function after the operator, and there was no exception, but it was useless - -!, this a still cannot be called by other methods, but I think it is a bit inappropriate for many people to call it an anonymous function!
The above is a brief analysis of the principle of Javascript self-executing anonymous function (function() { })() introduced to you by the editor. I hope it will be helpful to everyone!