diff --git a/lodash.js b/lodash.js index 53992eef1..2b3fd65f0 100644 --- a/lodash.js +++ b/lodash.js @@ -5760,7 +5760,13 @@ */ function after(n, func) { if (!isFunction(func)) { - throw new TypeError(FUNC_ERROR_TEXT); + if (isFunction(n)) { + var temp = n; + n = func; + func = temp; + } else { + throw new TypeError(FUNC_ERROR_TEXT); + } } n = nativeIsFinite(n = +n) ? n : 0; return function() { @@ -5788,7 +5794,13 @@ function before(n, func) { var result; if (!isFunction(func)) { - throw new TypeError(FUNC_ERROR_TEXT); + if (isFunction(n)) { + var temp = n; + n = func; + func = temp; + } else { + throw new TypeError(FUNC_ERROR_TEXT); + } } return function() { if (--n > 0) { diff --git a/test/test.js b/test/test.js index 526369826..a1f56c20e 100644 --- a/test/test.js +++ b/test/test.js @@ -800,6 +800,18 @@ deepEqual(actual, expected); }); + test('should allow `func` as the first argument', 1, function() { + var count = 0; + + try { + var after = _.after(function() { count++; }, 1); + after(); + after(); + } catch(e) {} + + strictEqual(count, 2); + }); + test('should not set a `this` binding', 2, function() { var after = _.after(1, function() { return ++this.count; }), object = { 'count': 0, 'after': after }; @@ -976,6 +988,18 @@ deepEqual(actual, expected); }); + test('should allow `func` as the first argument', 1, function() { + var count = 0; + + try { + var before = _.before(function() { count++; }, 2); + before(); + before(); + } catch(e) {} + + strictEqual(count, 1); + }); + test('should not set a `this` binding', 2, function() { var before = _.before(2, function() { return ++this.count; }), object = { 'count': 0, 'before': before };