Kwakwani ECMAScript Function Object (Class)

ECMAScript hanyar a hanyar da yana iya yin kwarin a hanyar da yana iya yin kwarin.

Function object (kasta)

ECMAScript mafi kwarin a hanyar da zai iya yin kwarin kamar hanyar da hanyar a hanyar da yana iya yin kwarin.

Function kasta za a hanyar a hanyar da kwarin donin kwarin da ke kiyasta.

Gyara a hanyar a Function kasta dona koyi hanyar:

var function_name = new function(arg1, arg2, ..., argN, function_body)

在上面的形式中,每个 arg 都是一个参数,最后一个参数是函数主体(要执行的代码)。这些参数必须是字符串。

记得下面这个函数吗?

function sayHi(sName, sMessage) {
  alert("Hello " + sName + sMessage);
}

还可以这样定义它:

var sayHi 
= 
new Function("sName", "sMessage", "alert(\"Hello \" + sName + sMessage);");

虽然由于字符串的关系,这种形式写起来有些困难,但有助于理解函数只不过是一种引用类型,它们的行为与用 Function 类明确创建的函数行为是相同的。

请看下面这个例子:

function doAdd(iNum) {
  alert(iNum + 20);
}
function doAdd(iNum) {
  alert(iNum + 10);
}
doAdd(10);	//输出 "20"

如你所知,第二个函数重载了第一个函数,使 doAdd(10) 输出了 "20",而不是 "30"。

如果以下面的形式重写该代码块,这个概念就清楚了:

var doAdd = new Function("iNum", "alert(iNum + 20)");
var doAdd = new Function("iNum", "alert(iNum + 10)");
doAdd(10);

请观察这段代码,很显然,doAdd 的值被改成了指向不同对象的指针。函数名只是指向函数对象的引用值,行为就像其他对象一样。甚至可以使两个变量指向同一个函数:

var doAdd = new Function("iNum", "alert(iNum + 10)");
var alsodoAdd = doAdd;
doAdd(10);	//输出 "20"
alsodoAdd(10);	//输出 "20"

在这里,变量 doAdd 被定义为函数,然后 alsodoAdd 被声明为指向同一个函数的指针。用这两个变量都可以执行该函数的代码,并输出相同的结果 - "20"。因此,如果函数名只是指向函数的变量,那么可以把函数作为参数传递给另一个函数吗?回答是肯定的!

function callAnotherFunc(fnFunction, vArgument) {
  fnFunction(vArgument);
}
var doAdd = new Function("iNum", "alert(iNum + 10)");
callAnotherFunc(doAdd, 10);	//Output "20"

In the above example, callAnotherFunc() has two parameters - the function to be called and the parameters passed to the function. This code passes doAdd() to the callAnotherFunc() function, with the parameter 10, and outputs "20".

Note:Although the Function constructor can be used to create functions, it is best not to use it because defining functions with it is much slower than with traditional methods. However, all functions should be considered instances of the Function class.

The length property of the Function object

As mentioned earlier, functions are reference types, so they also have properties and methods.

The length property defined by ECMAScript declares the number of parameters expected by the function. For example:

function doAdd(iNum) {
  alert(iNum + 10);
}
function sayHi() {
  alert("Hi");
}
alert(doAdd.length);	//Output "1"
alert(sayHi.length);	//Output "0"

The function doAdd() defines a parameter, so its length is 1; sayHi() does not define any parameters, so length is 0.

Remember, regardless of how many parameters are defined, ECMAScript can accept an arbitrary number of parameters (up to 25), which was explained in the 'Overview of Functions' chapter. The length property is just a convenient way to view the expected number of parameters by default.

Methods of the Function object

The Function object also has the valueOf() and toString() methods shared with all objects. Both methods return the source code of the function, which is particularly useful in debugging. For example:

function doAdd(iNum) {
  alert(iNum + 10);
}
document.write(doAdd.toString());

The following code outputs the text of the doAdd() function.Try it yourself!