Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to customize the function of JavaScript

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of JavaScript how to customize the function, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value, I believe you will gain something after reading this JavaScript how to customize the function article, let's take a look at it.

Js custom function is written as follows: 1, "function function name ([args]) {code}"; 2, "var function name = new Function (p1, p2,..., pn, body)"; 3, "function ([args]) {code}".

There are three ways to customize functions in JavaScript, even with function statements, using Function () constructors, and defining function literals.

Declaration function

Functions can be declared using function statements in JavaScript. The specific usage is as follows:

Function funName ([args]) {statements}

FunName is a function name, which, like a variable name, must be a legal identifier of JavaScript. After the function name is a list of parameters contained in parentheses, separated by commas. Parameters are optional and there is no limit to the number.

As identifiers, parameters are accessed only in the body of the function, and the parameters are private members of the function scope. When you call a function, you pass a value for the function, then use parameters to get the externally passed value, and interfere with the operation of the function inside the function.

After the parentheses is a curly bracket, and the statement contained in the braces is the main content of the function body structure. In the function weight, curly braces are essential, the lack of curly braces, JavaScript will throw syntax errors.

Example

The function statement must contain function names, parentheses, and curly braces, and other code can be omitted, so the simplest function body is an empty function.

Function funName () {} / / empty function

If you use an anonymous function, you can omit the function name.

Function () {} / / Anonymous empty function

Both var statements and function statements are declaration statements, and the variables and functions they declare are parsed during JavaScript precompilation, also known as variable promotion and function promotion. During the precompilation period, the JavaScript engine creates a context for each function, defines a variable object, and registers all parameters, private variables, and nested functions within the function as attributes on the variable object.

Function () constructor

You can quickly generate a function using the Function () constructor. The specific usage is as follows:

Var funName = new Function (p1, p2,..., pn, body)

The parameter types of Function () are strings, p1~pn represents the list of parameter names of the created function, and body represents the function structure statement of the created function, separated by semicolons between the body statements.

Example 1

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

Var f = new Function ("a", "b", "return aqb"); / / Clone the function structure through the constructor

In the above code, f is the name of the function you created. Functions are also defined, and functions with the same structure can be designed using function statements.

Function f (a, b) {/ / use the function statement to define the function structure return a + b;}

Example 2

Use the Function () constructor to create an empty function structure without specifying any arguments.

Var f = new Function (); / / define an empty function

Use the Function () constructor to dynamically create a function that does not restrict the user to the function body pre-declared by the function statement. Using the Function () constructor allows you to use the function as an expression rather than as a structure, so it's more flexible to use. The disadvantage is that the Function () constructor is compiled at execution time and is very inefficient and is generally not recommended.

Anonymous function (function direct)

The direct quantity of a function is also called an anonymous function, that is, a function does not have a function name, but only contains function keywords, parameters, and function body. The specific usage is as follows:

Function ([args]) {statements}

Example 1

The following code defines a function direct quantity.

Function (a, b) {/ / function directly measures 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 (/ / call the function as an Operand (function (a) b) {return a + b;}) (1, 2); / / return the value 3. This is the end of the article on "how to customize functions in JavaScript". Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "how to customize the function of JavaScript". If you want to learn more knowledge, you are welcome to 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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report