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 do functions in javascript declare

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 how to declare the function in javascript, which has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, let the editor take you to understand it.

In js, you can declare and define functions using the function keyword or the Function () function, with the syntax "function funName ([parameter list]) {...}" or "var funName = new Function ([parameter list, body])".

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

Declarative definition of functions in javascript

1. Use the function keyword

Syntax:

Function funName ([Parameter list]) {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.

2. Using the Function () function

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

Var funName = new Function ([parameter list, body])

The argument types of Function () are all strings, 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

Example 3

In the Function () constructor parameter, p1~pn is a list of parameter names, that is, p1 can represent not only a parameter, but also a comma-separated list of parameters. The following definition methods are equivalent.

Var f = new Function ("a", "b", "c", "return a+b+c"); var f = new Function ("a journal b, c", "return a+b+c"); var f = new Function ("a journal b", "c", "return a+b+c")

It is not very common to use the Function () constructor because a function body usually contains a lot of code, which can be poorly readable if passed as a line of strings.

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.

Thank you for reading this article carefully. I hope the article "how to declare functions in javascript" shared by the editor will be helpful to everyone. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

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