In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Most people do not understand the knowledge points of this article "how to define an anonymous function in javascript", so the editor summarizes the following, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to define an anonymous function in javascript" article.
In javascript, a function can have no name; a function without a name is called an "anonymous function", which contains only the function keyword, parameters, and function body, and the syntax is "function ([args]) {statements}".
The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.
In javascript, a function can have no name.
Functions that do not have a name are called anonymous functions and contain only the function keyword, parameters, and function body. The specific usage is as follows:
Function ([args]) {statements}
Example 1
The following code defines an anonymous function.
Function (a, b) {/ / anonymous function return a + b;}
In the above code, the function direct quantity is basically the same as defining the function structure using the function statement, and their structure is fixed. But the function direct quantity does not specify the function name, but directly uses the keyword function to represent the structure of the function, which is also called anonymous function.
Example 2
An anonymous function is an expression, that is, a function expression, not a statement of a function structure. Let's assign the anonymous function to the variable f as a value.
/ / assign the function as a value directly to the variable fvar f = function (a, b) {return a + b;}
When a function structure is assigned to a variable as a value, the variable can be called as a function, and the variable points to the anonymous function.
Console.log (f (1helm 2)); / / return value 3
Example 3
Anonymous functions, as values, can participate in more complex expression operations. For the above example, you can use the following code to complete the function definition and call integration operation.
Console.log (/ / calls the function as an Operand (function (a) b) {return a + b;}) (1, 2); / / returns the value 3
The role of anonymous functions:
1. Closures can be implemented through anonymous functions, which will be explained in a later article. Here is a brief introduction: a closure is a function that can access variables defined in the scope of a function. Anonymous functions are often needed to create a closure.
2. Simulate the block-level scope and reduce the global variables. After executing the anonymous function, the corresponding variables stored in memory will be destroyed, thus saving memory. Furthermore, in large multi-person development projects, the use of block-level scopes can greatly reduce the problem of naming conflicts, thus avoiding catastrophic consequences. From then on, developers no longer have to worry about messing up the global scope.
The above is about the content of this article on "how to define an anonymous function in javascript". I believe we all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.