1
0
Fork 0

[Bug Fix] Concrete class constructor is no longer invoked on extend

- The ctor must be instantiated for use in the prototype chain
- This was working in the past, but apparently no test existed for it and
  refactoring broke it
closure/master
Mike Gerwitz 2011-05-10 23:21:12 -04:00
parent 3c774a7b16
commit 91a47e4dcd
2 changed files with 33 additions and 0 deletions

View File

@ -327,6 +327,14 @@ function createConcreteCtor( cname, members )
initInstance( instance_id, this ); initInstance( instance_id, this );
this.__initProps(); this.__initProps();
// If we're extending, we don't actually want to invoke any class
// construction logic. The above is sufficient to use this class in a
// prototype, so stop here.
if ( extending )
{
return;
}
// call the constructor, if one was provided // call the constructor, if one was provided
if ( this.__construct instanceof Function ) if ( this.__construct instanceof Function )
{ {

View File

@ -348,3 +348,28 @@ for ( var i = 0; i < class_count; i++ )
}, Error, "Cannot redeclare method in same class definition" ); }, Error, "Cannot redeclare method in same class definition" );
} )(); } )();
/**
* To understand this test, one must understand how "inheritance" works
* with prototypes. We must create a new instance of the ctor (class) and add
* that instance to the prototype chain (if we added an un-instantiated
* constructor, then the members in the prototype would be accessible only
* though ctor.prototype). Therefore, when we instantiate this class for use in
* the prototype, we must ensure the constructor is not invoked, since our
* intent is not to create a new instance of the class.
*/
( function testConstructorShouldNotBeCalledWhenExtendingClass()
{
var called = false,
Foo = Class( {
'public __construct': function()
{
called = true;
}
} ).extend( {} );
assert.equal( called, false,
"Constructor should not be called when extending a class"
);
} )();