From fa9dbcbf2ef830c71febfbc5bac484077ef86782 Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Thu, 19 Jan 2012 23:21:04 -0500 Subject: [PATCH] [Fix #37] constructor property now properly set on instances --- lib/ClassBuilder.js | 11 ++++++----- test/test-class-constructor.js | 17 +++++++++++++++++ 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/lib/ClassBuilder.js b/lib/ClassBuilder.js index f8335e7..7eadf9a 100644 --- a/lib/ClassBuilder.js +++ b/lib/ClassBuilder.js @@ -388,11 +388,11 @@ exports.prototype.build = function extend( _, __ ) prototype, prop_init, members, new_class, this._classId ); - new_class.prototype = prototype; - new_class.constructor = new_class; - new_class.___$$props$$ = prop_init; - new_class.___$$methods$$ = members; - new_class.___$$sinit$$ = staticInit; + new_class.prototype = prototype; + new_class.prototype.constructor = new_class; + new_class.___$$props$$ = prop_init; + new_class.___$$methods$$ = members; + new_class.___$$sinit$$ = staticInit; attachFlags( new_class, props ); @@ -692,6 +692,7 @@ exports.prototype.createConcreteCtor = function( cname, members ) ) ; } + }; // provide a more intuitive string representation diff --git a/test/test-class-constructor.js b/test/test-class-constructor.js index 89ab46a..87d717d 100644 --- a/test/test-class-constructor.js +++ b/test/test-class-constructor.js @@ -167,3 +167,20 @@ assert.ok( }, TypeError, "Constructor cannot be private" ); } )(); + +/** + * When a constructor is instantiated, the instance's 'constructor' property is + * set to the constructor that was used to instantiate it. The same should be + * true for class instances. + * + * This will also be important for reflection. + */ +( function testConsructorPropertyIsProperlySetToClass() +{ + var Foo = Class( {} ); + + assert.ok( Foo().constructor === Foo, + "Instance constructor should be set to class" + ); +} )(); +