From b3795d193a92c440dabca6a4353628de2f353c7f Mon Sep 17 00:00:00 2001 From: Jake Rosoman Date: Tue, 17 Dec 2013 22:48:17 +1300 Subject: [PATCH] add default `child` support --- index.js | 21 +++++++++++++-------- test/test.js | 11 ++++++++++- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/index.js b/index.js index dc27844..459ce9e 100644 --- a/index.js +++ b/index.js @@ -24,16 +24,21 @@ function extensible(A){ }; /** - * make `B` inherit from `this`. Unless `final`, - * `B` will also be made extensible. + * make `child` inherit from `this`. Unless `final`, + * `child` will also be made extensible. If you don't + * pass a `child` a new one will be created. * - * @param {Function} B + * @param {Function} [child] * @param {Boolean} [final] - * @return {B} + * @return {child} */ -function extend(B, final){ +function extend(child, final){ + var A = this; + var B = 'function' != typeof child + ? function(){ A.apply(this, arguments); } + : child; !final && extensible(B); - inherit(B, this); - return B -}; \ No newline at end of file + inherit(B, A); + return B; +}; diff --git a/test/test.js b/test/test.js index ff44a68..55a8ccb 100644 --- a/test/test.js +++ b/test/test.js @@ -31,4 +31,13 @@ describe('A.extend(B, final)', function(){ assert(new B instanceof A); assert(B.extend === undefined); }) -}) \ No newline at end of file +}) + +describe('A.extend()', function(){ + it('should create a new function', function(){ + var A = extensible(function(){ this.touched = true; }); + var B = A.extend(); + assert(new B() instanceof A); + assert(new B().touched === true); + }) +})