The methods to create a function are: 1. Use the "var function name = new Function (parameter list, body);" statement; 2. Use the "function function name ([parameter list]) {...} " statement; 3. Use the "var function name=function([parameter list]){...}" statement.
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
Function (function)
⑴The function is also an object
⑵Some functions (code) can be encapsulated in the function, These functions (code) can be executed when needed
⑶Some codes can be saved in the function and called when needed
⑷When using typeof to check a function object, function
# will be returned
##⑸Three ways to create a function:
- ①Constructor
- ②Function declaration
- ③Function expression
Function() constructor
Use Function() constructor to quickly generate function. The specific usage is as follows:
var funName = new Function(p1, p2, ..., pn, body);
Copy after login
The parameter types of Function() are all strings, p1~pn represents the parameter name list of the created function, body represents the function structure statement of the created function, between the body statements Separate with semicolons.
Example:
Declaring functions
In JavaScript, you can use the function statement to declare functions. The specific usage is as follows:
function funName([args]) {
statements
}
Copy after login
funName is the function name, which like the variable name must be a legal JavaScript identifier. Following the function name is a list of parameters enclosed in parentheses and separated by commas. Parameters are optional and there is no limit on the number.
As identifiers, parameters are only accessed within the function body, and parameters are private members of the function scope. When calling a function, pass a value to the function, then use parameters to obtain the externally passed value, and intervene in the running of the function within the function body.
After the parentheses is a brace. The statement contained in the brace is the main content of the function body structure. In the function body, curly braces are essential. Without curly braces, JavaScript will throw a syntax error.
Example
The function statement must contain the function name, parentheses and braces, and other codes can be omitted, so the simplest function body is an empty function.
function funName() {} //空函数
Copy after login
If using an anonymous function, the function name can be omitted.
function () {} //匿名空函数
Copy after login
The var statement and the function statement are both declaration statements, and the variables and functions they declare are parsed when JavaScript is precompiled, also known as variable promotion and function promotion. During the pre-compilation period, the JavaScript engine creates a context for each function, defines a variable object, and registers all formal parameters, private variables, and nested functions in the function as attributes on the variable object.
Function expression
Syntax:
var 函数名 = function([args]){
statements
}
Copy after login
Specific example:
[Related Recommended:
javascript video tutorial, web front-end】
The above is the detailed content of What are the ways to create functions in javascript. For more information, please follow other related articles on the PHP Chinese website!