The differences between the more common functions defined in JavaScript are mainly explained through the following three aspects. Friends in need can refer to it
1: Call keyword function to construct
For example:
function distance(x1,x2,y1,y2) { var dx=x2-x1; var dy=y2-y1; return Math.sqrt(dx*dx+dy*dy); }
2: Use Function() constructor
For example:
var f=new Function*"x","y","return x*y");
This line of code creates a new function that is essentially equivalent to a function defined using the syntax you are familiar with:
function f(x,y) { return x*y; }
The Functino() constructor can accept any number of string parameters. Its last parameter is the body of the function, which can contain any JavaScript statements separated by semicolons. The other parameters are strings used to describe the names of the formal parameters to be defined by the function. If the function you define has no parameters, you can just pass a string (the body of the function) to the constructor.
Note that none of the parameters passed to the constructor Function() specify the name of the function it is creating. Unnamed functions created with the Function() constructor are sometimes called "anonymous functions".
You may be very curious to know what the Function() constructor is used for. Why can't we just use function statements to define all functions? The reason is that the Function() constructor allows us to dynamically build and compile a function without limiting us to the precompiled function body of the function statement. The side effect of this is that every time a function is called, the Function() constructor must compile it. Therefore, we should not call this constructor frequently in loop bodies or in frequently used functions.
Another reason to use the Function() constructor is that it can define the function as part of a JavaScript expression instead of defining it as a statement. In this case, using it is more convenient and even elegant. .
3: Function literal
A function literal is an expression that can define an anonymous function. The syntax of a function literal is very similar to that of a function statement, except that it is used as an expression rather than a statement, and there is no need to specify a function name. The following three lines of code define three basically the same functions using the function() statement, the Function() constructor, and the function literal:
function f(x){return x*x}; var f=new Function("x","return x*x;"); var f=function(x){reurn x*x};
Although the function literal creates an unnamed function, its syntax also specifies that it can specify a function name, which is very useful when writing a recursive function that calls itself.
For example:
var f=function fact(x){if(x<=1)return 1;else return x*fact(x-1);};
The above code defines an unnamed function and stores a reference to it in the variable f. It does not actually create a function named fact(), it just allows the function body to refer to itself by this name. Note, however, that this named function literal was not implemented correctly in versions prior to JavaScript 1.5.
The usage of function literals is very similar to the method of creating functions using the Function() constructor. Since they are created by JavaScript expressions rather than statements, the way they are used is more flexible, especially for functions that are only used once and do not need to be named. For example, a function specified using a function literal expression can be stored in a variable, passed to other functions, or even called directly:
a[0]=function(x){return x*x;};//定义一个函数并保存它 a.sort(function(a,b){return a-b;});//定义一个函数;把它传递给另一个函数 var tensquared=(function(x){return x*x;})(10);
和Function()构造函数一样,函数直接量创建的是未命名函数,而且不会自动地将这个函数存储在属性中。但是,比起Function()构造函数来说,函数直接量有一个重要的优点。由Function()构造函数创建的函数的主体必须用一个字符串说明,用这种方式来表达一个长而复杂的函数是狠笨拙的。但是函数直接量的主体使用的却是标准的JavaScript语法。而且函数直接量只被解析一次,而作为字符串传递给Function()构造函数的JavaScript代码则在每次调用构造函数时只需被解析一次和编译一次。
在JavaScript1.1中,可以使用构造函数Function()来定义函数,在JavaScript1.2和其后的版本中,还可以使用函数直接量来构造函数。你应该注意这两种方法之间的重要差别。
首先,构造函数Function()允许在运行时动态地创建和编译JavaScript代码。但是函数直接量却是函数结构的一个静态部分,就像function语句一样。
其次,作为第一个差别的必然结果,每次调用构造函数Function()时都会解析函数体并且创建一个新东汉数对象。如果对构造函数的调用出现在一个循环中,或者出现在一个经常被调用的函数中,这种方法的效率非常低。另一个方面,函数直接量或出现在循环和函数中的嵌套函数不是在每次调用时都被重新编译,而且每当遇到一个函数直接量时也不创建一个新的函数对象。
Function()构造函数和函数之间量之间的第三点差别是,使用构造函数Function()创建的函数不使用词法作用域,相反的,它们总是被当作顶级函数来编译,就像下面代码所说明的那样:
var y="global"; function constructFunction() { var y="local"; return new Function("return y");//不捕捉局部作用域。 } //这行代码将显示"global",因为Function()构造函数返回的函数并不使用局部作用域。 //假如使用一个函数直接量,这行代码则可能显示"local"。 alert(constructFunction());