Recently in the study of Pomelo source code, this framework is based on node. JS, so it is not frequently to deal with JavaScript scripts. So, in this article, we summarize
The main purpose and various existence forms of anonymous functions in JavaScript language. In fact, anonymous functions are available in many languages, and this word should not be unfamiliar to you.
First, function and anonymous function
First, let's look at the syntax for normal function definitions in javascript:
function functionname (var1,var2,..., Varx) {//... function Body}
As shown above, the definition of a normal function requires a function name to identify the function object, and with this identifier we can call it elsewhere (scoped)
。
So, what is an anonymous function? Anonymous Anonymous is no name, in this case there is no function name. So is it possible to remove the function name of the above code directly? The answer is
Obviously, it's negative. Because the above is not an execution expression, but a function definition body, directly remove the function name will be error.
The following code will report "function statement requires a name" error.
Although the above code is an error, we still cannot deny that it is an anonymous function, but the appearance of anonymous functions and the form of execution have some requirements, and the above is
is not in line with the use of one of the requirements, the details of our introduction later.
II. Benefits of anonymous functions
The benefits of an anonymous function are at least as follows two points:
1. Reduce the pollution of global variables (or variables within a scope);
2. Make the code structure more graceful, such as code can achieve block distribution.
For the above two points, I believe members are not difficult to understand. For the 2nd, we know that many other languages tend to provide support for local variables in block languages (for example, local variable definitions within the {} block in C), whereas the techniques of anonymous functions can be used in JS to implement the above functions similar to those in other high-level languages.
Iii. various forms of execution of anonymous functions
1. Use the parentheses operator (grouping operation) to make the anonymous function perform the definition and execute the defined function.
(function () {} ());
2. Use the parentheses operator (grouping operation) to make the anonymous function perform the definition before executing the function
(function () {}) ();
3. Make an anonymous function define and execute by adding an operator (such as!,+,-, etc.) before function
function () {} ();
4. Executing anonymous functions with new instantiation of objects
New function () {};
5. With the Void keyword to make anonymous functions execute
void function () {} ();
In fact, when we change new and void to other keywords, such as delete, you will find that the code is still performing well. In other words, as long as we add the
The keyword of the method can let it not error and execution.
Iv. Summary
There is no way to learn the new framework must be adapted to the developer's technical background and design concepts and so on. Our programming habits lie in ourselves, and we have to run behind others to learn the way we know him first. Note: The various JavaScript anonymous functions used in the above will be encountered when you read almost all JS-based source code.
This article is from the "Green Peak" blog, please make sure to keep this source http://zhuxianzhong.blog.51cto.com/157061/1616456
Various forms of execution of JavaScript anonymous functions