1
0
Fork 0

Ensured abstract classes cannot be instantiated and permitted their instantiation during extending so that it may be used in the subclass's prototype

closure/master
Mike Gerwitz 2010-11-14 20:48:39 -05:00
parent 746a9ee53d
commit 0d1ba74415
2 changed files with 52 additions and 11 deletions

View File

@ -211,6 +211,14 @@ function prop_copy( props, dest, result_data )
} }
/**
* Set to TRUE when class is being extended to allow the instantiation of
* abstract classes (for use in prototypes)
*
* @var {boolean}
*/
var extending = false;
/** /**
* Mimics class inheritance * Mimics class inheritance
* *
@ -225,18 +233,13 @@ function prop_copy( props, dest, result_data )
*/ */
function extend() function extend()
{ {
// ensure we'll be permitted to instantiate abstract classes for the base
extending = true;
var args = Array.prototype.slice.call( arguments ), var args = Array.prototype.slice.call( arguments ),
props = args.pop() || {}, props = args.pop() || {},
base = args.pop() || Class, base = args.pop() || Class,
prototype = new base();
prototype = new base(),
new_class = function()
{
if ( this.__construct instanceof Function )
{
this.__construct.apply( this, arguments );
}
};
// copy the given properties into the new prototype // copy the given properties into the new prototype
var result_data = { var result_data = {
@ -247,11 +250,37 @@ function extend()
// reference to the parent prototype (for more experienced users) // reference to the parent prototype (for more experienced users)
prototype.parent = base.prototype; prototype.parent = base.prototype;
var new_class = ( result_data.abstractMethods.length === 0 )
? (
// concrete class
function()
{
if ( this.__construct instanceof Function )
{
// call the constructor
this.__construct.apply( this, arguments );
}
}
)
: (
// do not allow abstract classes to be instantiated
function ()
{
if ( !extending )
{
throw new Error( "Abstract classes cannot be instantiated" );
}
}
);
// set up the new class // set up the new class
setup_props( new_class, result_data ); setup_props( new_class, result_data );
new_class.prototype = prototype; new_class.prototype = prototype;
new_class.constructor = new_class; new_class.constructor = new_class;
// we're done with the extension process
extending = false;
return new_class; return new_class;
} }

View File

@ -133,3 +133,15 @@ assert.equal(
"Subtypes of abstract types are not abstract if they provide concrete " + "Subtypes of abstract types are not abstract if they provide concrete " +
"implementations of all abstract methods" "implementations of all abstract methods"
); );
assert.throws( function()
{
new AbstractFoo();
new SubAbstractFoo();
}, Error, "Abstract classes cannot be instantiated" );
assert.ok(
new ConcreteFoo(),
"Concrete subclasses can be instantiated"
);