【发布时间】:2017-07-17 04:38:29
【问题描述】:
我正在查看 angular 的注入器代码,但无法理解这一行
Function.prototype.bind.apply(ctor, args)
代码。为什么我们要调用apply for bind?是不是像打电话申请申请一样?
我在一些问题上读到它可以用来调用任意参数的函数,但这可以通过arguments 对象来完成,对吧?
有人可以解决这个问题吗?
实际角码:
function instantiate(Type, locals, serviceName) {
// Check if Type is annotated and use just the given function at n-1 as parameter
// e.g. someModule.factory('greeter', ['$window', function(renamed$window) {}]);
var ctor = (isArray(Type) ? Type[Type.length - 1] : Type);
var args = injectionArgs(Type, locals, serviceName);
// Empty object at position 0 is ignored for invocation with `new`, but required.
args.unshift(null);
return new (Function.prototype.bind.apply(ctor, args))();
}
【问题讨论】:
-
args 可能是一个数组。
-
是的,申请数组并调用参数以便将ctor分配为this
标签: javascript angularjs