apply調用函數,並用指定對象替換函數的this值,同時用指定數組替換函數的參數。
functionName.apply([thisObj[,argArray]])
如果argArray為無效值,則會拋出"Object expected"錯誤;如果thisObj和argArray都沒有提供,則會使用當前this作為thisObj
function callMe(arg1, arg2) { var s = ""; s += "this value: " + this; s += "<br />"; for (i in callMe.arguments) { s += "arguments: " + callMe.arguments[i]; s += "<br />"; } return s;}document.write("Original function: <br/>");document.write(callMe(1, 2));document.write("<br/>");document.write("Function called with apply: <br/>");document.write(callMe.apply(3, [4, 5]));document.write("<br/>");document.write("Function called with apply with invalid array: <br/>");try{ document.write(callMe.apply(3,2));} catch (e) { document.write(e.message);}document.write("<br/><br/>");document.write("Function called with apply without any argument: <br/>");document.write(callMe.apply());//Output result://Original function: //this value: [object Window]// arguments: 1// arguments: 2//Function called with apply: //this value: 3// arguments: 4// arguments: 5//Function called with apply with invalid array: //Function.prototype.apply: Arguments list has wrong type//Function called with apply without any argument: //this value: [object Window]
call調用一個對象的方法,用另一個對象替換當前對象。
call([thisObj[, arg1[, arg2[, [, argN]]]]])
它允許您將函數的 this 對象從初始上下文變為由 thisObj 指定的新對象。 如果沒有提供 thisObj 參數,則 global 對象被用作 thisObj。與apply方法唯一不同的地方是,apply的第二個參數類型必須是Array,而call方法是將所有的參數列舉出來,用逗號分隔。
function callMe(arg1, arg2){ var s = ""; s += "this value: " + this; s += "<br />"; for (i in callMe.arguments) { s += "arguments: " + callMe.arguments[i]; s += "<br />"; } return s;}document.write("Original function: <br/>");document.write(callMe(1, 2));document.write("<br/>");document.write("Function called with call: <br/>");document.write(callMe.call(3, 4, 5));// Output: // Original function: // this value: [object Window]// arguments: 1// arguments: 2// Function called with call: // this value: 3// arguments: 4// arguments: 5
bind對於給定函數,建立具有與原始函數相同的主體的綁定函數。 在綁定功能中,this對象解析為傳入的對象。 該綁定函數具有指定的初始參數。
function.bind(thisArg[,arg1[,arg2[,argN]]])
其中function, thisArg為必選項。返回一個與 function 函數相同的新函數,只不過函數中的this對象和參數不同。
// Define the original function.var checkNumericRange = function (value) { if (typeof value !== ‘number‘) return false; else return value >= this.minimum && value <= this.maximum;}// The range object will become the this value in the callback function.var range = { minimum: 10, maximum: 20 };// Bind the checkNumericRange function.var boundCheckNumericRange = checkNumericRange.bind(range);// Use the new function to check whether 12 is in the numeric range.var result = boundCheckNumericRange (12);document.write(result);// Output: true以下代碼示範如何使用 arg1[,arg2[,argN]]] 參數。 該綁定函數將 bind 方法中指定的參數用作第一個參數和第二個參數。 在調用該綁定函數時,指定的任何參數將用作第三個、第四個參數(依此類推)。
// Define the original function with four parameters.var displayArgs = function (val1, val2, val3, val4) { document.write(val1 + " " + val2 + " " + val3 + " " + val4);}var emptyObject = {};// Create a new function that uses the 12 and "a" parameters// as the first and second parameters.var displayArgs2 = displayArgs.bind(emptyObject, 12, "a");// Call the new function. The "b" and "c" parameters are used// as the third and fourth parameters.displayArgs2("b", "c");// Output: 12 a b c 在對象定義內部使用bind方法可以將某個事件綁定到對象內部的某個方法,<input type="button" id="start" value="Start" /><input type="button" id="stop" value="Stop" /><script type="text/javascript"> function Car(owner) { this.owner = owner; this.start = function () { //start the car console.log(this); //output: Car {owner: "Mike", start: function, stop: function} check.html:14 console.log(this.owner + "‘s car is starting."); //output: Mike‘s car is starting. }; this.stop = function () { console.log(this); //output: <input type="button" id="stop" value="Stop" /> console.log(this.owner + "‘s car is starting."); //output: undefined‘s car is stopping. }; } var btnStart = document.getElementById("start"), btnStop = document.getElementById("stop"), someCar = new Car("Mike"); if (document.attachEvent) { btnStart.attachEvent("onClick", someCar.start.bind(someCar)); btnStop.attachEvent("onClick", someCar.stop); } else if (document.addEventListener) { btnStart.addEventListener("click", someCar.start.bind(someCar), false); btnStop.addEventListener("click", someCar.stop, false); }</script>從上面Sample我們發現,當不使用bind方法的時候,事件裡面的this指向的觸發click事件dom元素input,它當然沒有owner屬性;如果利用bind指定事件裡面的this對象,就能達到我們想要的效果。