JavaScript 的 Function 類型實(shí)際上是對(duì)象。每個(gè)函數(shù)都是 Function 類型的實(shí)例,而且而且與其他類型一樣都有屬性和方法。
由于函數(shù)是對(duì)象,因此函數(shù)名實(shí)際上也是一個(gè)指向函數(shù)對(duì)象的指針,不會(huì)與某個(gè)函數(shù)綁定。
兩種定義形式:
// 形式一
function sum1(num1, num2) {
return num1 + num2;
}
// 形式二
var sum2 = function(num1, num2) {
return num1 + num2;
}
因?yàn)楹瘮?shù)名僅僅是指向函數(shù)的指針,所以可以我們可以像下面這樣使用它:
function sum1(num1, num2) {
return num1 + num2;
}
Component.onCompleted: {
var sum2 = sum1;
var result = sum2(1, 2);
console.log("result: \t", result);
}
函數(shù)內(nèi)部屬性
在函數(shù)內(nèi)部,有兩個(gè)特殊的對(duì)象: arguments 和 this。其中,arguments 是一個(gè)類數(shù)組對(duì)象,包含著傳入函數(shù)中的所有參數(shù)。雖然 arguments 的主要用途是保存函數(shù)參數(shù),但是這個(gè)對(duì)象還有一個(gè)名叫 callee 的屬性,該屬性是一個(gè)指針,指向擁有這個(gè) arguments 對(duì)象的函數(shù)。
下面是一個(gè)階乘函數(shù):
function factorial(num) {
if (num <=1 ) {
return 1;
} else {
return num * factorial(num - 1);
}
}
上面這段遞歸將函數(shù)的執(zhí)行與函數(shù)名耦合在了一起。為了消除這種緊密的耦合可以像下面這樣使用 arguments.callee
function factorial(num) {
if (num <=1 ) {
return 1;
} else {
return num * arguments.callee(num - 1);
}
}
Component.onCompleted: {
var otherFactorial = factorial;
var result = otherFactorial(5);
console.log("result: \t", result);
}
這樣,無(wú)論調(diào)用時(shí)使用的是什么函數(shù)名,都可以保證正常完成遞歸調(diào)用。
函數(shù)內(nèi)的另一個(gè)特殊對(duì)象是 this,其行為與 Java 和 C# 中的 this 大致相似。換句話說(shuō),this 引用的是函數(shù)據(jù)以執(zhí)行的環(huán)境對(duì)象 —— 或者也可以說(shuō)是 this 值。下面是一段偏向 QML 形式的 JavaScript 代碼:
property string color: "red"
property var object: new Object
function sayColor() {
console.log("color: \t", this.color);
}
Component.onCompleted: {
object.color = "blue";
sayColor();
object.sayColor = sayColor;
object.sayColor();
}
上述代碼輸出結(jié)果:

一定要牢記,函數(shù)的名字僅僅是一個(gè)包含指針的變量而已。因此,即使是在不同的環(huán)境中執(zhí)行,全局的 sayColor() 函數(shù)與 object.sayColor() 指向的仍然是同一個(gè)函數(shù)。
函數(shù)屬性和方法
每個(gè)函數(shù)都包含兩個(gè)屬性:length 和 prototype。其中,length 屬性表示函數(shù)希望接收的命名參數(shù)的個(gè)數(shù)。如下面實(shí)例:
function sayHi(name) {
console.log("Hi,", name);
}
function sum(num1, num2) {
return num1 + num2;
}
function getColor() {
return "red";
}
Component.onCompleted: {
console.log("sayHi length: \t", sayHi.length);
console.log("sum length: \t", sum.length);
console.log("getColor length: \t", getColor.length);
}
輸出結(jié)果是:

每個(gè)函數(shù)都包含兩個(gè)非繼承而來(lái)的方法 apply() 和 call()。
apply() 和 call() 其實(shí)差不多,唯一不同的是,使用 call() 時(shí),傳遞給函數(shù)的參數(shù)必須逐個(gè)列舉出來(lái)。
下面是使用實(shí)例:
function sum(num1, num2) {
return num1 + num2;
}
function callSum1(num1, num2) {
// call() 方法就不能這么用了
return sum.apply(this, arguments);
}
function callSum2(num1, num2) {
return sum.apply(this, [num1, num2]);
}
function callSum(num1, num2) {
return sum.call(this, num1, num2);
}
Component.onCompleted: {
console.log("callSum1: \t", callSum1(1, 2));
console.log("callSum2: \t", callSum2(2, 3));
console.log("callSum: \t\t", callSum(3, 4));
}
輸出結(jié)果:

事實(shí)上,傳遞參數(shù)并非 apply() 和 call() 真正的用武之地,它們真正強(qiáng)大的地方是能夠擴(kuò)充函數(shù)賴以運(yùn)行的作用域。下面是一個(gè)實(shí)例:
Window {
id: window
property string color: "red"
property var o: new Object()
function sayColor() {
console.log("color: \t", this.color);
}
Component.onCompleted: {
o.color = "blue";
sayColor.call(this);
sayColor.call(window);
sayColor.call(o);
}
}
上述代碼的輸出:

從上例中我們可以看出使用 apply() 和 call() 的好處是對(duì)象不需要與方法有任何耦合關(guān)系。
函數(shù)還提供了 bind() 方法,這個(gè)方法會(huì)創(chuàng)建一個(gè)函數(shù)實(shí)例,其 this 值會(huì)被綁定到傳給 bind() 函數(shù)的值。例如:
Window {
id: window
property string color: "red"
property var o: new Object;
function sayColor() {
console.log("color: \t", this.color);
}
Component.onCompleted: {
o.color = "blue";
var objectSayColor = sayColor.bind(o);
objectSayColor();
}
}
將會(huì)輸出:

在這里,sayColor 調(diào)用 bind 并傳入對(duì)象 o,創(chuàng)建了 objectSayColor 函數(shù)。objectSayColor 函數(shù)的 this 值等于 o,因此即使是在全局作用域中調(diào)用這個(gè)函數(shù),也會(huì)看到輸出 “blue”。