From 1ee270883e50cc4ea021a3156d5a069d27055dec Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Wed, 10 Nov 2010 19:02:52 -0500 Subject: [PATCH] Began implementing constructor (not yet operable - test will fail) --- lib/class.js | 11 +++++++++-- test/test-class.js | 23 ++++++++++++++++++++++- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/lib/class.js b/lib/class.js index 5449b01..e25ab08 100644 --- a/lib/class.js +++ b/lib/class.js @@ -50,9 +50,16 @@ var extend = function() base = args.pop() || Class, prototype = new base(), - new_class = function() {}; + new_class = function() + { + if ( this.__construct instanceof Function ) + { + this.__construct.apply( this, arguments ); + } + }; - new_class.prototype = prototype; + new_class.prototype = prototype; + new_class.constructor = new_class; return new_class; } diff --git a/test/test-class.js b/test/test-class.js index 2c8c35b..869ad77 100644 --- a/test/test-class.js +++ b/test/test-class.js @@ -33,7 +33,14 @@ assert.ok( // create a basic test class -var Foo = Class.extend(); +var construct_count = 0; +var Foo = Class.extend( +{ + __construct: function() + { + construct_count++; + }, +}); assert.ok( @@ -45,3 +52,17 @@ assert.ok( ( Foo.prototype.extend instanceof Function ), "Created class contains extend method in 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" +); +