Skip to content

Commit

Permalink
Add ability to call __base in __constructor in case of inheritance fr…
Browse files Browse the repository at this point in the history
…om plane function (close #9)
  • Loading branch information
dfilatov committed Mar 5, 2014
1 parent adef165 commit 4817d19
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
10 changes: 8 additions & 2 deletions lib/inherit.js
Original file line number Diff line number Diff line change
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);
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();
};
};

0 comments on commit 4817d19

Please sign in to comment.