In js, you can use the function keyword or the Function() function to declare and define functions, with the syntax "function funName([parameter list]){...}" or "var funName = new Function([ Parameter list, body])".
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
Declaration and definition of functions in javascript
1. Use the function keyword
syntax :
function funName([参数列表]) { statements }
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() {} //空函数
If using an anonymous function, the function name can be omitted.
function () {} //匿名空函数
The var statement and function statement are both declaration statements, and the variables and functions they declare are parsed during JavaScript precompilation, also known as variable promotion and function promotion. During the pre-compilation period, the JavaScript engine will create a context for each function, define a variable object, and register all formal parameters, private variables, and nested functions in the function as attributes on the variable object.
2. Use the Function() function
Use the Function() constructor to quickly generate a function. The specific usage is as follows:
var funName = new Function([参数列表,body]);
The parameter types of Function() are all strings, body represents the function structure statement of the created function, and the body statements are separated by semicolons.
Example 1
You can omit all parameters and only pass a string to represent the function body.
var f = new Function ("a", "b", "return a+b"); //通过构造函数来克隆函数结构
In the above code, f is the name of the function created. The same function is defined, and functions with the same structure can be designed using the function statement.
function f(a, b) { //使用function语句定义函数结构 return a + b; }
Example 2
Use the Function() constructor to create an empty function structure without specifying any parameters.
var f = new Function(); //定义空函数
Example 3
In the Function() constructor parameters, p1~pn is a list of parameter names, that is, p1 can not only represent one parameter, but also a comma-separated parameter. list. The following definition methods are equivalent.
var f = new Function("a", "b", "c", "return a+b+c"); var f = new Function("a, b, c", "return a+b+c"); var f = new Function("a,b", "c", "return a+b+c");
Using the Function() constructor is not very common because a function body usually includes a lot of code. If these codes are passed as a line of strings, the readability of the code will be very poor.
Use the Function() constructor to dynamically create functions. It does not limit users to the function body pre-declared by the function statement. Using the Function() constructor allows the function to be used as an expression rather than as a structure, so it is more flexible to use. The disadvantage is that the Function() constructor is compiled during execution, the execution efficiency is very low, and its use is generally not recommended.
[Related recommendations: javascript learning tutorial]
The above is the detailed content of What is used to declare functions in javascript. For more information, please follow other related articles on the PHP Chinese website!