call()與appy()
Call()方法
call() 方法是與經典的對象冒充方法最相似的方法。它的第一個參數用作 this 的對象。其他參數都直接傳遞給函數自身。例如:
1: function sayColor(sPrefix,sSuffix) {
2: alert(sPrefix + this.color + sSuffix);
3: };
4:
5: var obj = new Object();
6: obj.color = "blue";
7:
8: sayColor.call(obj, "The color is ", "a very nice color indeed.");
Apply()方法
apply() 方法有兩個參數,用作 this 的對象和要傳遞給函數的參數的數組。例如:
1: function sayColor(sPrefix,sSuffix) {
2: alert(sPrefix + this.color + sSuffix);
3: };
4:
5: var obj = new Object();
6: obj.color = "blue";
7:
8: sayColor.apply(obj, new Array("The color is ", "a very nice color indeed."));

浙公網安備 33010602011771號