What are the ways to create functions in javascript

青灯夜游
Release: 2022-03-08 18:44:12
Original
3007 people have browsed it

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.

What are the ways to create functions in javascript

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:

  • You can omit all parameters and only pass a string to represent the function body.

  • var f = new Function ("a", "b", "return a+b");  //通过构造函数来克隆函数结构
    Copy after login
  • You can create an empty function structure without specifying any parameters.

  • var f = new Function();  //定义空函数
    Copy after login

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:

What are the ways to create functions in javascript

[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!

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