Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to call __base in __constructor in case of inheritance from plane function #10

Merged
merged 2 commits into from
Mar 5, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions lib/inherit.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* @module inherit
* @version 2.1.0
* @version 2.2.0
* @author Filatov Dmitry <[email protected]>
*/

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -119,6 +123,8 @@ function inherit() {

extend(res, base);

res.__parent = base;

var basePtp = base.prototype,
resPtp = res.prototype = objCreate(basePtp);

Expand Down Expand Up @@ -166,4 +172,4 @@ if(typeof define === 'function') {

defineAsGlobal && (global.inherit = inherit);

})(this);
})(this);
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>",
Expand Down
18 changes: 17 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -191,4 +207,4 @@ exports.testFunctionMixinStatic = function(test) {

test.equal(B.staticMethodM(), 'M');
test.done();
};
};