1
0
Fork 0

Simply moved extend() function higher up in the source file, since it will be one of the most commonly referenced functions

closure/master
Mike Gerwitz 2010-11-15 06:54:37 -05:00
parent f9d7dca4b3
commit 095683ba42
1 changed files with 74 additions and 74 deletions

View File

@ -80,6 +80,80 @@ function Class() {};
/**
* 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
*
* 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 = true;
var args = Array.prototype.slice.call( arguments ),
props = args.pop() || {},
base = args.pop() || Class,
prototype = new base();
// copy the given properties into the new prototype
var result_data = {
abstractMethods: ( base.abstractMethods || [] ).slice()
};
prop_copy( props, prototype, result_data );
// 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
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;
}
/** /**
* Copies properties to the destination object * Copies properties to the destination object
* *
@ -277,80 +351,6 @@ function array_shrink( items )
} }
/**
* 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
*
* 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 = true;
var args = Array.prototype.slice.call( arguments ),
props = args.pop() || {},
base = args.pop() || Class,
prototype = new base();
// copy the given properties into the new prototype
var result_data = {
abstractMethods: ( base.abstractMethods || [] ).slice()
};
prop_copy( props, prototype, result_data );
// 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
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;
}
/** /**
* Sets up common properties for the provided function (class) * Sets up common properties for the provided function (class)
* *