diff --git a/lib/inherit.js b/lib/inherit.js index 4f642ab..ac9e3e2 100644 --- a/lib/inherit.js +++ b/lib/inherit.js @@ -69,7 +69,11 @@ function override(base, res, add) { if(isFunction(prop) && (!hasIntrospection || prop.toString().indexOf('.__base') > -1)) { res[name] = (function(name, prop) { - var baseMethod = base[name] || noOp; + var baseMethod = base[name]? + base[name] : + name === '__constructor'? // case of inheritance from plane function + res.__self.__parent : + noOp; return function() { var baseSaved = this.__base; this.__base = baseMethod; @@ -119,6 +123,8 @@ function inherit() { extend(res, base); + res.__parent = base; + var basePtp = base.prototype, resPtp = res.prototype = objCreate(basePtp); @@ -166,4 +172,4 @@ if(typeof define === 'function') { defineAsGlobal && (global.inherit = inherit); -})(this); \ No newline at end of file +})(this); diff --git a/test/test.js b/test/test.js index 6962c92..a71f7f9 100644 --- a/test/test.js +++ b/test/test.js @@ -66,6 +66,22 @@ exports.testInherit = function(test) { test.done(); }; +exports.testInheritFromPlaneFunction = function(test) { + var A = function(val) { + this.prop = val; + }, + B = inherit(A, { + __constructor : function() { + this.__base('fromB'); + } + }); + + test.ok(new B() instanceof A); + test.equal(new B().prop, 'fromB'); + test.done(); +}; + + exports.testStaticInherit = function(test) { var A = inherit({}, { method1 : function() { @@ -191,4 +207,4 @@ exports.testFunctionMixinStatic = function(test) { test.equal(B.staticMethodM(), 'M'); test.done(); -}; \ No newline at end of file +};