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
parent
095683ba42
commit
f632b93d64
81
lib/class.js
81
lib/class.js
|
@ -81,12 +81,19 @@ function Class() {};
|
|||
|
||||
|
||||
/**
|
||||
* Set to TRUE when class is being extended to allow the instantiation of
|
||||
* abstract classes (for use in prototypes)
|
||||
* Creates extend function
|
||||
*
|
||||
* @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 )
|
||||
{
|
||||
extending = false;
|
||||
|
||||
/**
|
||||
* Mimics class inheritance
|
||||
|
@ -100,7 +107,7 @@ var extending = false;
|
|||
*
|
||||
* @return {Object} extended class
|
||||
*/
|
||||
function extend()
|
||||
return function extend()
|
||||
{
|
||||
// ensure we'll be permitted to instantiate abstract classes for the base
|
||||
extending = true;
|
||||
|
@ -119,30 +126,9 @@ function extend()
|
|||
// reference to the parent prototype (for more experienced users)
|
||||
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
|
||||
var new_class = create_ctor( result_data, extending );
|
||||
|
||||
setup_props( new_class, result_data );
|
||||
new_class.prototype = prototype;
|
||||
new_class.constructor = new_class;
|
||||
|
@ -154,6 +140,45 @@ function extend()
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* 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 )
|
||||
{
|
||||
// call the constructor
|
||||
this.__construct.apply( this, arguments );
|
||||
}
|
||||
};
|
||||
}
|
||||
// abstract class
|
||||
else
|
||||
{
|
||||
return function ()
|
||||
{
|
||||
if ( !extending )
|
||||
{
|
||||
throw new Error( "Abstract classes cannot be instantiated" );
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
} )();
|
||||
|
||||
|
||||
/**
|
||||
* Copies properties to the destination object
|
||||
*
|
||||
|
|
Loading…
Reference in New Issue