Three methods defined function in JavaScript


The method of three definitions of functions are: a method function definition statements, expressions, and the amount of direct function Function () constructor. The following describes these methods in turn how to achieve specific.

1. The function definition statement
// summation function
function SUM (A, B) {
return A + B;
}
This is the most typical function declarations, function keyword beginning, followed by the function name, a pair of parentheses (contains the parameter names separated by commas 0 or more) and a pair of curly braces (JS contains 0 or more statements constitute the function body). This function is defined way need to explicitly specify the function name, before the code execution will be loaded into the interpreter scope, this feature allows us to define the function before it calls the function. We can verify this in code.
console.log (sum); // console output sum function source code, in which case the function is not yet defined
function sum (A, B) {
return A + B;
}
the console.log (sum (2,3)); // 5
As mentioned function declaration, we should mention the scope of the function. Function scope refers to all variables declared within a function is always visible in the body of the function, which means that variables that have been available before the declaration. This feature can be referred to the statement in advance, that all variables declared within a function, before the declaration has been defined, but will only be real when performing assigned to this variable. From code-can clearly see this

scope = var "Global";
function F () {
the console.log (scope); // Output "undefined", instead of "Global"
var scope = "local"; // this variable assigned an initial value, but the variable itself anywhere there is a function in vivo are defined
console.log (scope); // output "local"
}
F ();
the above code is equivalent to
var scope = "Global";
function F () {
var scope; // at the top of the function declaration of local variables, i.e., the advance statement
console.log (scope); // variable exists, the output of "undefined", instead of "Global"
var scope = "local"; // this variable initial value assigned
console. log (scope); // output "local"
}
F ();


2. The amount of expression function directly
@ factorial function
var factorial = function fact (x) {// a function assigned to a variable
IF (X <0) {return NaN3;}
the else IF (X === 0) return. 1 {;}
the else
return FACT X * (. 1-X); // recursive function
};
the console.log (factorial (. 3)); //. 6

As with the function definition statement, the amount of expression is also a direct function uses the keyword function. This definition is generally applicable to it as part of a larger expression, such as the definition function assignment and invocation. Function by direct function of the amount of generated function name can be omitted, and at that point an anonymous function. In the following example: This can make the code more compact. Function definition expression is particularly suited to define a function that will be used.

var f = function (x) {// anonymous function name of the function is omitted
return X * X;
}


And the function definition statement difference is that the expression is a function of the amount of direct load function only when the implementation of the code, we can use the following code to illustrate.

console.log (f); // console output undefined, yet this time loading function f
var f = function (x) { // begin loading function
return X * X;
}
console.log (f); // Control source code table output function


3. Function () Constructor
var f = new Function ( "x ", "y", "return x + y"); // Function () Constructor
var f = function (x, y ) {return x + y }; // this is equivalent to two codes

Function () constructor string can be passed to any number of arguments, argument a final text is indicated by the function thereof, can contain any number of JavaScript statements. If the function does not contain any configuration parameter, you only need to pass in a function body. And the first two different ways that, Function () constructor allows JavaScript to dynamically create and translation functions at runtime. Each call to Function () constructor function body will be parsed, and creates a new function object. Thus, the implementation of this constructor function loops or more calls, the efficiency will be affected. In contrast, recompiled when nested function definitions and function expressions cycle will not be executed every time.

Function () constructor is also worthy of note is that it creates a function not use lexical scoping, the compiler function body code always executed at the top level function. As shown in the following code:

var a = 3; // declare variables in the top-level function A
function F () {
var A = 2; // declare a local variable in a function in vivo A
return new new Function ( "A * A return;"); // can not be captured local scope
}
the console.log (F () ()); // 9 rather than 4 console output, describe the compiler constructor functions performed at the top level

We can Function () constructor considered eval executed in the global scope (). In actual programming, Function () constructor rarely used, the first two methods defined in more common use.

Guess you like

Origin www.cnblogs.com/login123/p/12056247.html