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',同时也可以选择将参数绑定到函数。 –