What is the anonymous method in javascript

青灯夜游
Release: 2023-01-07 11:44:15
Original
2249 people have browsed it

In JavaScript, an anonymous method is an anonymous function, which refers to a function without a function name and only contains the function keyword, parameters and function body; the syntax format is "function ([args]) {function body}". An anonymous function is an expression, that is, a function expression, not a statement of a function structure.

What is the anonymous method in javascript

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

Method (method) is a JavaScript function called through an object. In other words, methods are also functions, just special functions. Anonymous methods or anonymous functions in JavaScript are functions without a function name.

Anonymous method (anonymous function)

Anonymous function, that is, the function has no function name and only contains the function keyword, parameters and function body. The specific usage is as follows:

function ([args]) {
    statements
}
Copy after login

Example 1

The following code defines an anonymous function.

function (a, b) {  //匿名函数
    return a + b;
}
Copy after login

In the above code, function literals are basically the same as using function statements to define function structures, and their structures are fixed. However, the function literal does not specify a function name, but directly uses the keyword function to represent the structure of the function. This kind of function is also called an anonymous function.

Example 2

Anonymous function is an expression, that is, a function expression, not a statement of function structure. Next, assign the anonymous function as a value to the variable f.

//把函数作为一个值直接赋值给变量 f
var f = function (a, b) {
    return a + b;
};
Copy after login

When the 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(1,2));  //返回值3
Copy after login

Example 3

Anonymous functions serve as values ​​and can participate in more complex expression operations. For the above example, you can use the following code to complete the integrated operation of function definition and call.

console.log(  //把函数作为一个操作数进行调用
    (function (a,b) {
        return a + b;
    })(1, 2));  //返回数值3
Copy after login

[Recommended learning: javascript advanced tutorial]

The above is the detailed content of What is the anonymous method in javascript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template