有人能解释一下 _.before 函数是如何实现的吗?我不太理解为什么内部的 times
变量要跟踪每个函数调用。它不应该像普通函数那样在本地作用域中重置吗?
_.before 函数的代码:
// Returns a function that will only be executed up to (but not including) the Nth call.
_.before = function(times, func) {
var memo;
return function() {
if (--times > 0) {
memo = func.apply(this, arguments);
}
if (times <= 1) func = null;
return memo;
};
};
谢谢。