Home  >  Article  >  Web Front-end  >  Summary of several differences in javascript function definition_javascript skills

Summary of several differences in javascript function definition_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:05:261005browse

javascript函数的定义

1:调用关键字function来构造,如:
function distance(x1,x2,y1,y2)
{
var dx=x2-x1;
var dy=y2-y1;
return Math.sqrt(dx*dx+dy*dy);
}

2:使用Function()构造函数
var f=new Function*"x","y","return x*y");
这行代码创建了一个新函数,该函数和你所熟悉的语法定义的函数基本上时等价的:

function f(x,y)
{
return x*y;
}

Functino()构造函数可以接受任意多个字符串参数。它的最后一个参数时函数的主体,其中可以包含任何JavaScript语句,语句之间用分号分隔。其他的参数都是用来说明函数要定义的形式参数名的字符串。如果你定义的函数没有参数,那么可以只需给构造函数传递一个字符串(即函数的主体)即可。

注意,传递给构造函数Function()的参数中没有一个用于说明它要创建的函数名。用Function()构造函数创建的未命名函数有时被成为“匿名函数”。

你可能非常想知道Function()构造函数的用途是什么。为什么不能只用function语句来定义所有的函数呢?原因是Function()构造函数允许我们动态地建立和编译一个函数,它不会将我们限制在function语句预编译的函数体中。这样做带来的负面影响效应就是每次调用一个函数时,Function()构造函数都要对它进行编译。因此,在循环体中或者在经常使用的函数中,我们不应该频繁地调用这个构造函数。

使用Function()构造函数的另一个原因是它能够将函数定义为JavaScript表达式的一部分,而不是将其定义一个语句,这种情况下使用它就显得比较的方面,甚至可以说精致。

3:函数直接量
函数直接量是一个表达式,它可以定义匿名函数。函数直接量的语法和function语句非常相似,只不过它被用作表达式,而不是用作语句,而且也无需指定函数名。下面的三行代码分别使用function()语句、Funciont()构造函数和函数直接量定义了三个基本上相同的函数:

function f(x){return x*x};
var f=new Function("x","return x*x;");
var f=function(x){reurn x*x};

虽然函数直接量创建的是未命名函数,但是它的语法也规定它可以指定函数名,这在编写调用自身的递归函数时非常有用。例如:
var f=function fact(x){if(x<=1)return 1;else return x*fact(x-1);};
上面的代码定义了一个未命名函数,并对它的引用存储在变量f中。它并没有真正的创建一个名为fact()的函数,只是允许函数体用这个名字来引用自身。但是要注意,JavaScript1.5之前的版本中没有正确实现这种命名的函数直接量。

函数直接量的用法和用Function()构造函数创建函数的方法非常相似。由于它们都是由JavaScript的表达式创建的,而不是由语句创建的,所以使用它们的方式也就更加灵活,尤其适用于那些只使用一次,而且无需命名的函数。例如,一个使用函数直接量表达式指定的函数可以存储在一个变量中、传递给其他的函数甚至被直接调用:

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和其后的版本中,还可以使用函数直接量来构造函数。你应该注意这两种方法之间的重要差别。

First, the constructor Function() allows JavaScript code to be dynamically created and compiled at runtime. But function literals are a static part of the function structure, just like the function statement.

Secondly, as a corollary to the first difference, each time the constructor Function() is called, the function body will be parsed and a new Eastern Han numeral object will be created. This approach is very inefficient if the call to the constructor occurs in a loop or in a frequently called function. On the other hand, function literals or nested functions that appear within loops and functions are not recompiled on each call, and a new function object is not created each time a function literal is encountered.

The third difference between Function() constructors and functions is that functions created using the constructor Function() do not use lexical scope. Instead, they are always treated as top-level function to compile, as illustrated by the following code:

var y="global";
function constructFunction()
{
var y="local";
return new Function("return y");//Do not capture local effects domain.
}
//This line of code will display "global" because the function returned by the Function() constructor does not use local scope.
//If a function literal is used, this line of code may display "local".
alert(constructFunction());

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn