diff --git a/lib/inherit.js b/lib/inherit.js index 4f642ab..765769d 100644 --- a/lib/inherit.js +++ b/lib/inherit.js @@ -1,6 +1,6 @@ /** * @module inherit - * @version 2.1.0 + * @version 2.2.0 * @author Filatov Dmitry */ @@ -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/package.json b/package.json index 5b0d834..169bd4c 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name" : "inherit", - "version" : "2.1.0", - "description" : "Inheritance module for node", + "version" : "2.2.0", + "description" : "Inheritance module for Node.js and browsers", "homepage" : "https://github.com/dfilatov/node-inherit", "keywords" : ["class", "prototype", "inheritance", "mixins", "static"], "author" : "Dmitry Filatov ", 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 +};