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
87
lib/class.js
87
lib/class.js
|
@ -81,14 +81,21 @@ 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 )
|
||||||
|
{
|
||||||
|
extending = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Mimics class inheritance
|
* Mimics class inheritance
|
||||||
*
|
*
|
||||||
* This method will mimic inheritance by setting up the prototype with the
|
* This method will mimic inheritance by setting up the prototype with the
|
||||||
|
@ -100,8 +107,8 @@ var extending = false;
|
||||||
*
|
*
|
||||||
* @return {Object} extended class
|
* @return {Object} extended class
|
||||||
*/
|
*/
|
||||||
function extend()
|
return function extend()
|
||||||
{
|
{
|
||||||
// ensure we'll be permitted to instantiate abstract classes for the base
|
// ensure we'll be permitted to instantiate abstract classes for the base
|
||||||
extending = true;
|
extending = true;
|
||||||
|
|
||||||
|
@ -119,30 +126,9 @@ 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
|
||||||
|
var new_class = create_ctor( result_data, extending );
|
||||||
|
|
||||||
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;
|
||||||
|
@ -151,7 +137,46 @@ function extend()
|
||||||
extending = false;
|
extending = false;
|
||||||
|
|
||||||
return new_class;
|
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 )
|
||||||
|
{
|
||||||
|
// call the constructor
|
||||||
|
this.__construct.apply( this, arguments );
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
// abstract class
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return function ()
|
||||||
|
{
|
||||||
|
if ( !extending )
|
||||||
|
{
|
||||||
|
throw new Error( "Abstract classes cannot be instantiated" );
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} )();
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue