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

What are the methods for javascript to create functions?

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

Share

Shulou(Shulou.com)06/01 Report--

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

The methods to create functions are: 1, with "var function name = new Function (parameter list, body);" statement; 2, with "function function name ([parameter list]) {.}" statement; 3, with "var function name = function ([parameter list]) {...}" statement.

The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.

Function (function)

The ⑴ function is also an object

Some functions (code) can be encapsulated in the ⑵ function and can be executed when needed.

You can save some code in the ⑶ function to call when needed.

When ⑷ checks a function object using typeof, it returns function

There are three ways for ⑸ to create functions:

① constructor

② function declaration

③ function expression

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:

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

You can create an empty function structure without specifying any parameters.

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

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 expression

Syntax:

Var function name = function ([args]) {statements}

Specific examples:

This is the end of the article on "what are the ways for javascript to create functions?" Thank you for reading! I believe you all have a certain understanding of the knowledge of "what are the methods of creating functions in javascript". If you want to learn more, 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

Development

Wechat

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

12
Report