Method three definitions function in JavaScript

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/Nice_Nice_Nice/article/details/99934452

//
three methods in JavaScript function definitions.
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. 函数定义语句

    //求和函数
function sum(a,b){
	return a+b;
}

This is the most typical function declaration, beginning with the keyword function, followed by the function name, a pair of parentheses (containing the parameter name separated by zero or more comma) and a pair of curly braces (containing 0 JS 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);        //控制台输出sum函数的源代码,此时函数还未定义
function sum(a,b){
	return a+b;
}
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

var scope = "global";
function f(){
  console.log(scope);         //输出“undefined”,而不是“global”
  var scope = "local";          //变量在这里赋初始值,但变量本身在函数体内任何地方均是有定义的
  console.log(scope);         //输出“local”
}
f();

The above code is equivalent to

var scope = "global";
function f() {
  var scope;                //在函数顶部声明了局部变量,即声明提前
  console.log(scope);         //变量存在,输出“undefined”,而不是“global”
  var scope = "local";       //变量在这里赋初始值  
console.log(scope);         //输出“local”
}
f();
  1. A function of the amount of direct expression
    // factorial function evaluations

    var factorial = function fact(x){   //将函数赋值给一个变量
     	if(x<0) {return NaN;}
     	else if(x===0) {return 1;}
     	else
     	return x*fact(x-1);     //递归函数
     };
     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){    //省略函数名的匿名函数
  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);        //控制台输出undefined,此时函数f还未加载
var f=function(x){     //开始加载函数
  return x*x;
}
console.log(f);       //控制台输出函数的源代码
  1. Function () Constructor
  2. var f = new Function("x","y","return x+y"); //Function()构造函数 var f = function(x,y){return x+y}; //这两条代码是等价的

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;     //在顶层函数中声明变量a
function f(){
	var a = 2;   //在函数体内声明局部变量a
	return new Function("return a*a;");   //无法捕获局部作用域
}
console.log(f()());     //控制台输出9而非4,说明构造函数的编译在顶层函数执行

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.

----------------
Disclaimer: This article is CSDN blogger "crazyorange_shen 'original article, follow the CC 4.0 by-sa copyright agreement, reproduced, please attach the original source link and this statement. .
Original link: https: //blog.csdn.net/crazyorange_shen/article/details/68957987

Guess you like

Origin blog.csdn.net/Nice_Nice_Nice/article/details/99934452