diff --git a/lib/class.js b/lib/class.js index e25ab08..8034549 100644 --- a/lib/class.js +++ b/lib/class.js @@ -31,6 +31,16 @@ var Class = function() }; +var prop_copy = function( props, dest ) +{ + // copy each of the properties to the destination object + for ( property in props ) + { + dest[ property ] = props[ property ]; + } +} + + /** * Mimics class inheritance * @@ -58,6 +68,10 @@ var extend = function() } }; + // copy the given properties into the new prototype + prop_copy( props, prototype ); + + // set up the new class new_class.prototype = prototype; new_class.constructor = new_class; diff --git a/test/test-class.js b/test/test-class.js index 869ad77..42684e6 100644 --- a/test/test-class.js +++ b/test/test-class.js @@ -32,17 +32,21 @@ assert.ok( ); +// these two variables are declared outside of the class to ensure that they +// will still be set even if the context of the constructor is wrong +var construct_count = 0, + construct_context = null; + // create a basic test class -var construct_count = 0; var Foo = Class.extend( { __construct: function() { construct_count++; + construct_context = this; }, }); - assert.ok( ( Foo instanceof Object ), "Extend method creates a new object" @@ -53,16 +57,29 @@ assert.ok( "Created class contains extend method in prototype" ); +assert.ok( + ( Foo.prototype.__construct instanceof Function ), + "Provided properties should be copied to the new class prototype" +); + assert.equal( construct_count, 0, "Constructor should not be called before class is instantiated" ); + var obj = new Foo(); + assert.equal( construct_count, 1, "Constructor should be invoked once the class is instantiated" ); +assert.equal( + obj, + construct_context, + "Constructor should be invoked within the context of the class instance" +); +