Function.prototype.bind = function() {
var _this = this,
original = _this,
args = Array.prototype.slice.call(arguments),
_obj = args.shift(),
func = function() {
var _that = _obj;
return original.apply(_that, args.concat(
Array.prototype.slice.call(
arguments, args.length)));
};
func.bind = function() {
var args = Array.prototype.slice.call(arguments);
return Function.prototype.bind.apply(_this, args);
}
return func;
};
我知道這是一個綁定函數。但我不明白它在做什麼,特別是args.concat
部分。 concat
做什麼?另外,.bind
方法做什麼.apply
和.call
不能?這段代碼做了什麼?
'bind'返回一個函數,'call'和'apply'調用一個函數,它們是不同的動物。當你想要確保稍後使用的函數被特定的上下文調用時使用'bind',同時也可以選擇將參數綁定到函數。 –