From 095683ba429424ac60fc73b9e96c185f852e1a62 Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Mon, 15 Nov 2010 06:54:37 -0500 Subject: [PATCH] Simply moved extend() function higher up in the source file, since it will be one of the most commonly referenced functions --- lib/class.js | 148 +++++++++++++++++++++++++-------------------------- 1 file changed, 74 insertions(+), 74 deletions(-) diff --git a/lib/class.js b/lib/class.js index f9b5058..6a33b92 100644 --- a/lib/class.js +++ b/lib/class.js @@ -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 * @@ -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) *