diff --git a/lib/class.js b/lib/class.js index 07591a7..3c61fbf 100644 --- a/lib/class.js +++ b/lib/class.js @@ -175,6 +175,9 @@ var extend = ( function( extending ) // call the constructor, if one was provided if ( this.__construct instanceof Function ) { + // note that since 'this' refers to the new class (even + // subtypes), and since we're using apply with 'this', the + // constructor will be applied to subtypes without a problem this.__construct.apply( this, arguments ); } }; diff --git a/test/test-class-constructor.js b/test/test-class-constructor.js index b6adbcd..dc792d9 100644 --- a/test/test-class-constructor.js +++ b/test/test-class-constructor.js @@ -94,3 +94,36 @@ for ( var i = 0, len = args.length; i < len; i++ ) "Arguments should be passed to the constructor: " + i ); } + +var SubFoo = Foo.extend( +{ + args: [ 'should', 'be', 'overwritten' ], +} ); + +construct_count = 0; +construct_context = null; + +var args2 = [ 'fried', 'pickle' ], + subobj = new SubFoo( args2[ 0 ], args2[ 1 ] ); + +assert.equal( + construct_count, + 1, + "Parent constructor should be called for subtype if not overridden" +); + +assert.equal( + construct_context, + subobj, + "Parent constructor is run in context of the subtype" +); + +// this should be implied by the previous test, but let's add it for some peace +// of mind +assert.ok( + ( ( subobj.args[ 0 ] === args2[ 0 ] ) + && ( subobj.args[ 1 ] == args2[ 1 ] ) + ), + "Parent constructor sets values on subtype" +); +