Several types of functions are defined in JavaScript: 1. Use the function keyword to define functions through declarations or expressions, and the syntax is "function functionName(parameters){executed code}"; 2. Use the function constructor Function() to construct the function .
The operating environment of this tutorial: Windows 10 system, JavaScript version 1.8.5, Dell G3 computer.
JavaScript uses the keyword function to define functions.
A function can be defined by a declaration or it can be an expression.
Function Declaration
In the previous tutorial, you already learned the syntax of function declaration:
function functionName(parameters) { Executed code}
The function will not be executed immediately after it is declared, but will be called when we need it.
Function constructor
The function constructor is used to create a function object. In JavaScript, actually every function is a function object.
Syntax of function constructor:
new Function ([arg1[, arg2[, ...argN]],] functionBody)
arg1, arg2, ... argN are used as variable names as normal parameter names of the function. These variable names must Is a string representing a single variable or a comma-separated string representing multiple variables that conforms to the JavaScript variable identification specification, such as "x", "theValue", or "a,b".
functionBody A string containing the JavaScript statement that defines the function.
Description
1. The function object is created when the function constructor is parsed into a function. However, this will be less efficient than using function declarations, function expressions, etc., because these functions are directly parsed through the code.
2. The string parameters passed to the function constructor will be used as the variable parameter names of the function generated by the function constructor, and the order in which they appear will be used as the order of the parameters of the generated function.
3. Calling the function constructor as a function (whether modified with the new keyword or not) has the same effect.
Example
// Example can be run directly in your JavaScript console // Create a function that takes two arguments and returns the sum of those arguments var adder = new Function('a', 'b', 'return a + b'); // Call the function adder(2, 6); // > 8
Parameters a and b are normal parameter names that can be used in the function body.
The generated function is:
function (a,b){undefined return a+b; }
About a way of parsing json: var json = (new Function("return " + str))();
The principle is the same,
the generated function is:
function toJson(str){undefined return str; } var str="{'a':1,'b':'abc'}"; toJson(str);
that is: function(){return {'a':1,'b':'abc'}}
The difference between function constructor and function declaration:
functions created with function constructor will not be created in the context Closures are always created in the global scope. When executing the created function, they can only use their own local variables or global variables, which is different from eval.
Related recommendations: JavaScript learning tutorial.
The above is the detailed content of the several types of JavaScript defined functions. For more information, please pay attention to other related articles on the PHP Chinese website!