【发布时间】:2014-11-05 14:17:39
【问题描述】:
我有一个关于annotated underscore.js source code的问题:
上面写着:
返回传入回调的有效(对于当前引擎)版本的内部函数,以在其他下划线函数中重复应用。
var createCallback = function(func, context, argCount) {
if (context === void 0) return func;
switch (argCount == null ? 3 : argCount) {
case 1: return function(value) {
return func.call(context, value);
};
case 2: return function(value, other) {
return func.call(context, value, other);
};
case 3: return function(value, index, collection) {
return func.call(context, value, index, collection);
};
case 4: return function(accumulator, value, index, collection) {
return func.call(context, accumulator, value, index, collection);
};
}
return function() {
return func.apply(context, arguments);
};
};
明确地说,这个代码块的效率是如何达到的?
因为我看到使用包装器,创建了一层间接层,作用域链又增长了一层。
我需要了解如何达到效率,以便在我自己的 js 中应用这些技巧。
谢谢!
【问题讨论】:
-
感谢@nikoshr 的编辑!
标签: javascript performance underscore.js