Ensured abstract classes cannot be instantiated and permitted their instantiation during extending so that it may be used in the subclass's prototype
parent
746a9ee53d
commit
0d1ba74415
51
lib/class.js
51
lib/class.js
|
@ -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()
|
||||||
{
|
{
|
||||||
var args = Array.prototype.slice.call( arguments ),
|
// ensure we'll be permitted to instantiate abstract classes for the base
|
||||||
props = args.pop() || {},
|
extending = true;
|
||||||
base = args.pop() || Class,
|
|
||||||
|
|
||||||
prototype = new base(),
|
var args = Array.prototype.slice.call( arguments ),
|
||||||
new_class = function()
|
props = args.pop() || {},
|
||||||
{
|
base = args.pop() || Class,
|
||||||
if ( this.__construct instanceof Function )
|
prototype = new base();
|
||||||
{
|
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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"
|
||||||
|
);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue