1
0
Fork 0

Refactored constructor creation logic for new classes into its own method and placed both functions (and their shared var) into a closure to remove the 'extending' var from the module's scope

closure/master
Mike Gerwitz 2010-11-15 07:07:49 -05:00
parent 095683ba42
commit f632b93d64
1 changed files with 78 additions and 53 deletions

View File

@ -81,77 +81,102 @@ function Class() {};
/** /**
* Set to TRUE when class is being extended to allow the instantiation of * Creates extend function
* abstract classes (for use in prototypes)
* *
* @var {boolean} * The 'extending' parameter is used to override the functionality of abstract
* class constructors, allowing them to be instantiated for use in a subclass's
* prototype.
*
* @param {boolean} extending whether a class is currently being extended
*
* @return {Function} extend function
*/ */
var extending = false; var extend = ( function( extending )
/**
* Mimics class inheritance
*
* This method will mimic inheritance by setting up the prototype with the
* provided base class (or, by default, Class) and copying the additional
* properties atop of it.
*
* The class to inherit from (the first argument) is optional. If omitted, the
* first argument will be considered to be the properties list.
*
* @return {Object} extended class
*/
function extend()
{ {
// ensure we'll be permitted to instantiate abstract classes for the base extending = false;
extending = true;
var args = Array.prototype.slice.call( arguments ), /**
props = args.pop() || {}, * Mimics class inheritance
base = args.pop() || Class, *
prototype = new base(); * This method will mimic inheritance by setting up the prototype with the
* provided base class (or, by default, Class) and copying the additional
* properties atop of it.
*
* The class to inherit from (the first argument) is optional. If omitted, the
* first argument will be considered to be the properties list.
*
* @return {Object} extended class
*/
return function extend()
{
// ensure we'll be permitted to instantiate abstract classes for the base
extending = true;
// copy the given properties into the new prototype var args = Array.prototype.slice.call( arguments ),
var result_data = { props = args.pop() || {},
abstractMethods: ( base.abstractMethods || [] ).slice() base = args.pop() || Class,
}; prototype = new base();
prop_copy( props, prototype, result_data );
// reference to the parent prototype (for more experienced users) // copy the given properties into the new prototype
prototype.parent = base.prototype; var result_data = {
abstractMethods: ( base.abstractMethods || [] ).slice()
};
prop_copy( props, prototype, result_data );
var new_class = ( result_data.abstractMethods.length === 0 ) // reference to the parent prototype (for more experienced users)
? ( prototype.parent = base.prototype;
// concrete class
function() // set up the new class
var new_class = create_ctor( result_data, extending );
setup_props( new_class, result_data );
new_class.prototype = prototype;
new_class.constructor = new_class;
// we're done with the extension process
extending = false;
return new_class;
}
/**
* Creates the constructor for a new class
*
* This constructor will call the __constructor method for concrete classes
* and throw an exception for abstract classes (to prevent instantiation).
*
* @param {Object} result_data data from property copy operation
*
* @return {Function} constructor
*/
function create_ctor( result_data )
{
// concrete class
if ( result_data.abstractMethods.length === 0 )
{
return function()
{ {
if ( this.__construct instanceof Function ) if ( this.__construct instanceof Function )
{ {
// call the constructor // call the constructor
this.__construct.apply( this, arguments ); this.__construct.apply( this, arguments );
} }
} };
) }
: ( // abstract class
// do not allow abstract classes to be instantiated else
function () {
return function ()
{ {
if ( !extending ) if ( !extending )
{ {
throw new Error( "Abstract classes cannot be instantiated" ); throw new Error( "Abstract classes cannot be instantiated" );
} }
} };
); }
}
// set up the new class } )();
setup_props( new_class, result_data );
new_class.prototype = prototype;
new_class.constructor = new_class;
// we're done with the extension process
extending = false;
return new_class;
}
/** /**