先贴代码:
var executeBound = function(sourceFunc, boundFunc, context, callingContext, args) {
if (!(callingContext instanceof boundFunc))
return sourceFunc.apply(context, args);
var self = baseCreate(sourceFunc.prototype);
var result = sourceFunc.apply(self, args);
if (_.isObject(result))
return result;
return self;
};
// Create a function bound to a given object (assigning this, and arguments,
// optionally). Delegates to ECMAScript 5's native Function.bind if
// available.
.bind = function(func, context) {
if (nativeBind && func.bind === nativeBind) return nativeBind.apply(func, slice.call(arguments, 1));
if (!.isFunction(func)) throw new TypeError('Bind must be called on a function');
var args = slice.call(arguments, 2);
var bound = function() {
return executeBound(func, bound, context, this, args.concat(slice.call(arguments)));
};
return bound;
};
想问下,executeBound(func, bound, context, this, args.concat(slice.call(arguments)))
这个里面的this是否就是指代调用_.bound的对象,然后后面为何要做(!(callingContext instanceof boundFunc))
这个判断,网上查了好久,没查出来,请高手指导下,非常感谢
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。
这个得看如何调用的,因为this对象可以通过call和apply进行更改,如果不是通过apply或者call执行调用的,那么就是指向bound的实例对象了
instanceof是判断对象是否属于某一类型。如果你不判断,可能会出错。因为要调用这个类型的方法,不是这个类型的实例可能方法不存在而出错