As you can imagine, there are many ways to execute anonymous functions immediately. To summarize, there are three typical ways (there are other ways, please let us know):
Method one
(function() {
//…
})();
Method 2
void function() {
//…
}();
Method three
~function() {
//…
}();
You may have seen Method 1 and Method 2. Here we mainly explain Method 3. Before explaining the third method, we now review the definition of the operator "~" (bit negation) in EMCAScript (page 72 of the fifth edition), a simple translation:
Convert the old value to a 32-bit integer by combining statements with operators. Execute the statement after the operator and convert the line. The result is a 32-bit integer and returned.
As can be understood from the above, in fact, bitwise operators can immediately return the value of the following expression. In fact, other bitwise operators can achieve this purpose, for example:
!function() {
//…
}();
etc. can achieve our goal. So there is actually no other reason for using "~", it's just that the code "looks good" :^)
Efficiency is not so much the efficiency of executing anonymous functions in three ways, but it is better to directly analyze the execution efficiency of each operator. So let’s compare and take a look at the definitions of “()” and “void” in the EMCA specification
Group Operators (fifth edition, page 66)
Returns the execution result of an expression
void (page 70, 5th edition)
Combining statements with operators returns undefined
Since the group operator also needs to execute the statement and return the value returned by the statement block, compared to void, there will be multiple operations to obtain the statement block (although it does not consume much performance), so in this case the performance of void is better than that of the group operator .
Comparing the two, the performance comparison of method three is obviously lower than the first two. To sum up, from a grammatical perspective, among the above three anonymous function methods, method two is better than method one, and method three is the least efficient.
Summarize and think about the advantages between the three
Method 1 is very common and safe, so no one will blame you for using this method. However, students who use method 1 may often make the "problem" of forgetting to match brackets (especially when the statement block is very long. , I often get it wrong)
Using bitwise operators to execute anonymous functions is very trendy and is used to look cool. Many IDEs (such as IDEA) and syntax highlighting tools do not support the third way of writing.
void is the most efficient, but it always feels very bloated compared to the other two implementations (just a few more characters?)
So
Considering the amount of code and efficiency, it is correct to use method one in extreme cases where extra code needs to be saved, and use method three to give priority to efficiency. Then I would like to explain the use of method two here. In fact, the difference in efficiency between the three methods is very big. Small. Therefore, it is almost untenable to use that method solely based on efficiency.
The specific plan to adopt needs to be considered based on the actual situation. For example, I often use method three because 1. It’s convenient (just add one character) 2. Matching brackets when the function is long will be dizzy 3. It’s cool to use, but method 3 often makes me look at it Code team members are troubled.
If you have framework-based basic code such as some class libraries, using method 1 is the safest and easy for everyone to understand, so it is the safest choice.
[Original text: http://www.gracecode.com/archives/3004/ Very good, everyone can learn from it