先看一個經常遇到的場景:你想給 document 的 mousemove 綁定物件自己的方法,但直接寫 this.move 會發現,方法裡的 this 根本不指向那個物件。
var Mouse = function () {
// Look! no that = this!
this.position = [0, 0];
if (document.addEventListener) {
document.addEventListener('mousemove', ?); //this.move?
} else if (document.attachEvent) {
document.attachEvent("onmousemove", ?); //this.move? 怎麼放進去
}
};
Mouse.prototype.move = function (arg1, arg2, event) {
event = window.event || event;
var x = event.pageX || event.offsetX,
y = event.pageY || event.offsetY;
this.position = position = [x, y];
this.log(arg1, arg2);
};
Mouse.prototype.log = function (arg1, arg2) {
console.log(arg1 + "," + arg2);
console.log(this.position);
};
new Mouse();
那個問號處到底該寫什麼?答案就是 Function.prototype.bind。IE6~8 不支援它,現代瀏覽器則都已內建,所以我們接下來要做的是「模仿」它。
(function () {
var proxy = function (fn, target) {
var proxy = function () {
if (2 < arguments.length) { //存在被代理的函數有參數的時候
var privateArgs = Array.prototype.slice.call(arguments, 2);
//從第三個開始取出來,組裝成 [this, 綁定的對象, 參數列表]
return function () {
var args = Array.prototype.slice.call(arguments);
//這裡的 arguments 與外面的不是同一個,
//它是被代理函數內部的 arguments,
//比如 move 函數裡 arguments[0] 就是事件對象 e
Array.prototype.unshift.apply(args, privateArgs);
//把傳進來的參數補在前面,就實現了和原生 bind 一樣的參數形式
//例如 a = new Mouse(); a.move(1, 2);
return fn.apply(target, args);
}
}
return function () {
return fn.apply(target, arguments);
}
}
return proxy.apply(null, arguments);
};
/* 支持原生的使用原生的 */
Function.prototype.bind = Function.prototype.bind ||
function (target) {
//這裡的 this 指代要被代理的函數
if (1 < arguments.length) {
var args = Array.prototype.slice.call(arguments, 1); //取出參數列表
args.unshift(this, target); //args 變成 [this, 綁定的對象, 參數列表]
return proxy.apply(null, args);
}
return proxy(this, target);
};
})();
這段代碼之所以繞,是因為要處理「參數預綁」:呼叫 bind 時傳入的參數,必須排在呼叫時傳入的參數之前,順序才和原生行為一致。核心技巧就是 unshift——把私有參數塞到參數列表的最前面。
if (document.addEventListener) {
document.addEventListener('mousemove', this.move.bind(this, 1, 2));
} else if (document.attachEvent) {
document.attachEvent("onmousemove", this.move.bind(this, 1, 2));
}
以後凡是遇到「要加事件,又希望方法裡的 this 指向別的對象」,這招都很好用。
var a = function () {
console.log(arguments[0]); //1
console.log(arguments[1]); //2
console.log(this.key1);
//這樣綁定參數的話,我的參數列出來才能和原生的 bind 一樣,就這麼簡單
};
var b = {
key1: "value1"
};
a.bind(b, 1, 2)();
網路上常見的一種簡化寫法看起來沒問題,但其實丟掉了參數:
Function.prototype.bind = function (target) {
var self = this;
return function () {
return self.apply(target, arguments); //這裡的 arguments 根本傳不進來
}
}
var a = function () {
console.log(arguments.length); //這樣 bind 的話,arguments 參數失效
//arguments.length = 0
console.log(this.key1);
};
var b = { key1: "value1" };
a.bind(b, [1, 2], 3)(); //期望 arguments.length = 2,實際卻是 0
問題在於:這個版本只轉發了「呼叫時」傳入的參數,卻把 bind 時預綁的參數丟掉了。原生的 bind 會把兩者合併,這才是它強大的地方。很多人覺得這段代碼是對的,其實不然。
(function () {
var proxy = function (fn, target) {
var proxy = function () {
if (2 < arguments.length) {
var privateArgs = Array.prototype.slice.call(arguments, 2);
return function () {
var args = Array.prototype.slice.call(arguments);
Array.prototype.unshift.apply(args, privateArgs);
return fn.apply(target, args);
}
}
return function () {
return fn.apply(target, arguments);
}
}
return proxy.apply(null, arguments);
};
Function.prototype.bind = Function.prototype.bind ||
function (target) {
if (1 < arguments.length) {
var args = Array.prototype.slice.call(arguments, 1);
args.unshift(this, target);
return proxy.apply(null, args);
}
return proxy(this, target);
};
})();
var Mouse = function () {
if (document.addEventListener) {
document.addEventListener('mousemove', this.move.bind(this, 1, 2, [3, 4]));
} else if (document.attachEvent) {
document.attachEvent("onmousemove", this.move.bind(this, 1, 2, [3, 4]));
}
};
Mouse.prototype.move = function () {
console.log(arguments[arguments.length - 1].clientX);
};
把三個關鍵片段放在一起看,就一目了然了:
var privateArgs = Array.prototype.slice.call(arguments, 2);
//私有的參數,代表代理者的參數,這裡是 1、2、[3,4]
return function () {
var args = Array.prototype.slice.call(arguments);
//這裡的參數代表被代理者的參數,也就是事件函數內部的 e
Array.prototype.unshift.apply(args, privateArgs);
//把兩者的參數合並,私有參數在前,目的是和原生的參數順序一致
return fn.apply(target, args);
//把合並後的參數(1、2、[3,4]、e)傳進去並 apply
}
用這種方式包裝之後,還能省掉一句麻煩的兼容處理。原本我們都習慣寫:
event = window.event || event;
但既然事件對象永遠是最後一個參數,直接寫 arguments[arguments.length - 1] 就能兼容所有瀏覽器的事件對象,省下不少代碼與思考。
寫這篇的目的,是希望大家對 arguments 有真正的理解——它不是陣列、它和形參之間有那層微妙的連動關係、以及為什麼 ES6 要用 rest 參數(...args)來取代它。真的看懂了,你會發現原生的 bind 之所以那樣設計,每一行都有它的道理。