标签:
_.bind(function, object, [*arguments]) :绑定function到object,任何时候调用函数,都指向这个object。不能绑定两个对象。没看明白
其实apply和call也能改变this的指定,只是bind之后,可以省略掉apply和call
_.bind = function bind(func, context) {
var bound, args;
if (func.bind === nativeBind && nativeBind) return nativeBind.apply(func, slice.call(arguments, 1));
if (!_.isFunction(func)) throw new TypeError;
args = slice.call(arguments, 2);
return bound = function() {
//什么时候this instanceof bound = true?
if (!(this instanceof bound)) return func.apply(context, args.concat(slice.call(arguments)));
ctor.prototype = func.prototype;
var self = new ctor;
var result = func.apply(self, args.concat(slice.call(arguments)));
if (Object(result) === result) return result;
return self;
};
};
标签:
原文地址:http://www.cnblogs.com/wang-jing/p/4737609.html