2010-11-10 17:41:12 -05:00
|
|
|
/**
|
|
|
|
* Contains basic inheritance mechanism
|
|
|
|
*
|
|
|
|
* Copyright (C) 2010 Mike Gerwitz
|
|
|
|
*
|
2010-11-10 22:07:03 -05:00
|
|
|
* This file is part of ease.js.
|
2010-11-10 17:41:12 -05:00
|
|
|
*
|
2010-11-10 22:07:03 -05:00
|
|
|
* ease.js is free software: you can redistribute it and/or modify it under the
|
|
|
|
* terms of the GNU Lesser General Public License as published by the Free
|
|
|
|
* Software Foundation, either version 3 of the License, or (at your option)
|
|
|
|
* any later version.
|
2010-11-10 17:41:12 -05:00
|
|
|
*
|
2010-11-10 22:07:03 -05:00
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
|
|
|
* for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
2010-11-10 17:41:12 -05:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* @author Mike Gerwitz
|
|
|
|
* @package core
|
|
|
|
*/
|
|
|
|
|
2011-03-27 02:02:04 -04:00
|
|
|
var util = require( __dirname + '/util' ),
|
2011-03-27 23:04:40 -04:00
|
|
|
class_builder = require( __dirname + '/class_builder' )
|
2011-03-06 12:38:31 -05:00
|
|
|
;
|
|
|
|
|
2010-11-10 17:41:12 -05:00
|
|
|
|
2011-03-03 19:43:20 -05:00
|
|
|
/**
|
|
|
|
* This module may be invoked in order to provide a more natural looking class
|
|
|
|
* definition mechanism
|
|
|
|
*
|
|
|
|
* This may not be used to extend existing classes. To extend an existing class,
|
|
|
|
* use the class's extend() method. If unavailable (or extending a non-ease.js
|
|
|
|
* class/object), use the module's extend() method.
|
|
|
|
*
|
2011-03-04 00:17:25 -05:00
|
|
|
* @param {string=} name optional name
|
|
|
|
* @param {Object} def class definition
|
2011-03-03 19:43:20 -05:00
|
|
|
*
|
|
|
|
* @return {Class} new class
|
|
|
|
*/
|
2011-03-03 22:33:18 -05:00
|
|
|
module.exports = function()
|
2011-03-03 19:43:20 -05:00
|
|
|
{
|
2011-03-04 19:49:15 -05:00
|
|
|
var type = typeof arguments[ 0 ],
|
|
|
|
result = null
|
2011-03-04 00:24:42 -05:00
|
|
|
;
|
2011-03-03 22:33:18 -05:00
|
|
|
|
2011-03-04 00:24:42 -05:00
|
|
|
switch ( type )
|
2011-03-03 19:43:20 -05:00
|
|
|
{
|
2011-03-04 00:24:42 -05:00
|
|
|
// anonymous class
|
|
|
|
case 'object':
|
2011-03-04 19:49:15 -05:00
|
|
|
result = createAnonymousClass.apply( null, arguments );
|
2011-03-04 00:24:42 -05:00
|
|
|
break;
|
2011-03-03 19:43:20 -05:00
|
|
|
|
2011-03-04 00:24:42 -05:00
|
|
|
// named class
|
|
|
|
case 'string':
|
2011-03-04 19:49:15 -05:00
|
|
|
result = createNamedClass.apply( null, arguments );
|
2011-03-04 00:24:42 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
// we don't know what to do!
|
|
|
|
throw TypeError(
|
|
|
|
"Expecting anonymous class definition or named class definition"
|
|
|
|
);
|
2011-03-03 19:43:20 -05:00
|
|
|
}
|
|
|
|
|
2011-03-04 19:49:15 -05:00
|
|
|
return result;
|
2011-03-03 19:43:20 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2010-11-10 23:30:30 -05:00
|
|
|
/**
|
|
|
|
* Creates a class, inheriting either from the provided base class or the
|
|
|
|
* default base class
|
|
|
|
*
|
|
|
|
* @param {Object} base object to extend (extends Class by default)
|
|
|
|
*
|
|
|
|
* @return {Object} extended class
|
|
|
|
*/
|
2011-03-03 19:43:20 -05:00
|
|
|
module.exports.extend = function( base )
|
2010-11-10 23:30:30 -05:00
|
|
|
{
|
|
|
|
return extend.apply( this, arguments );
|
2010-12-28 22:10:12 -05:00
|
|
|
};
|
2010-11-10 23:30:30 -05:00
|
|
|
|
|
|
|
|
2010-12-29 23:33:30 -05:00
|
|
|
/**
|
|
|
|
* Implements an interface or set of interfaces
|
|
|
|
*
|
|
|
|
* @param {...Interface} interfaces interfaces to implement
|
|
|
|
*
|
|
|
|
* @return {Class} new class containing interface abstractions
|
|
|
|
*/
|
2011-03-03 19:43:20 -05:00
|
|
|
module.exports.implement = function()
|
2010-12-29 23:33:30 -05:00
|
|
|
{
|
2011-03-05 02:59:21 -05:00
|
|
|
// implement on empty base
|
|
|
|
return createImplement(
|
2011-03-16 19:24:02 -04:00
|
|
|
null,
|
2011-03-05 02:59:21 -05:00
|
|
|
Array.prototype.slice.call( arguments )
|
|
|
|
);
|
2010-12-29 23:33:30 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2010-12-28 20:58:42 -05:00
|
|
|
/**
|
|
|
|
* Determines whether the provided object is a class created through ease.js
|
|
|
|
*
|
|
|
|
* @param {Object} obj object to test
|
|
|
|
*
|
|
|
|
* @return {boolean} true if class (created through ease.js), otherwise false
|
|
|
|
*/
|
2011-03-03 19:43:20 -05:00
|
|
|
module.exports.isClass = function( obj )
|
2010-12-28 20:58:42 -05:00
|
|
|
{
|
|
|
|
obj = obj || {};
|
|
|
|
|
2011-03-27 23:04:40 -04:00
|
|
|
return ( obj.prototype instanceof class_builder.ClassBase )
|
2010-12-28 20:58:42 -05:00
|
|
|
? true
|
|
|
|
: false
|
|
|
|
;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines whether the provided object is an instance of a class created
|
|
|
|
* through ease.js
|
|
|
|
*
|
|
|
|
* @param {Object} obj object to test
|
|
|
|
*
|
|
|
|
* @return {boolean} true if instance of class (created through ease.js),
|
|
|
|
* otherwise false
|
|
|
|
*/
|
2011-03-03 19:43:20 -05:00
|
|
|
module.exports.isClassInstance = function( obj )
|
2010-12-28 20:58:42 -05:00
|
|
|
{
|
|
|
|
obj = obj || {};
|
|
|
|
|
2011-03-27 23:04:40 -04:00
|
|
|
return ( obj instanceof class_builder.ClassBase )
|
2010-12-28 20:58:42 -05:00
|
|
|
? true
|
|
|
|
: false;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2010-12-29 21:13:21 -05:00
|
|
|
/**
|
|
|
|
* Determines if the class is an instance of the given type
|
|
|
|
*
|
|
|
|
* The given type can be a class, interface, trait or any other type of object.
|
|
|
|
* It may be used in place of the 'instanceof' operator and contains additional
|
|
|
|
* enhancements that the operator is unable to provide due to prototypal
|
|
|
|
* restrictions.
|
|
|
|
*
|
|
|
|
* @param {Object} type expected type
|
|
|
|
* @param {Object} instance instance to check
|
|
|
|
*
|
|
|
|
* @return {boolean} true if instance is an instance of type, otherwise false
|
|
|
|
*/
|
2011-03-27 23:04:40 -04:00
|
|
|
module.exports.isInstanceOf = class_builder.isInstanceOf;
|
2010-12-29 21:13:21 -05:00
|
|
|
|
|
|
|
|
2010-12-29 21:37:11 -05:00
|
|
|
/**
|
|
|
|
* Alias for isInstanceOf()
|
|
|
|
*
|
|
|
|
* May read better in certain situations (e.g. Cat.isA( Mammal )) and more
|
|
|
|
* accurately conveys the act of inheritance, implementing interfaces and
|
|
|
|
* traits, etc.
|
|
|
|
*/
|
2011-03-03 19:43:20 -05:00
|
|
|
module.exports.isA = module.exports.isInstanceOf;
|
2010-12-29 21:37:11 -05:00
|
|
|
|
|
|
|
|
2011-03-04 19:49:15 -05:00
|
|
|
/**
|
|
|
|
* Creates a new anonymous Class from the given class definition
|
|
|
|
*
|
|
|
|
* @param {Object} def class definition
|
|
|
|
*
|
|
|
|
* @return {Class} new anonymous class
|
|
|
|
*/
|
|
|
|
function createAnonymousClass( def )
|
|
|
|
{
|
|
|
|
// ensure we have the proper number of arguments (if they passed in
|
|
|
|
// too many, it may signify that they don't know what they're doing,
|
|
|
|
// and likely they're not getting the result they're looking for)
|
|
|
|
if ( arguments.length > 1 )
|
|
|
|
{
|
|
|
|
throw Error(
|
|
|
|
"Expecting one argument for anonymous Class definition; " +
|
|
|
|
arguments.length + " given."
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return extend( def );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new named Class from the given class definition
|
|
|
|
*
|
|
|
|
* @param {string} name class name
|
|
|
|
* @param {Object} def class definition
|
|
|
|
*
|
|
|
|
* @return {Class} new named class
|
|
|
|
*/
|
|
|
|
function createNamedClass( name, def )
|
|
|
|
{
|
|
|
|
// if too many arguments were provided, it's likely that they're
|
|
|
|
// expecting some result that they're not going to get
|
|
|
|
if ( arguments.length > 2 )
|
|
|
|
{
|
|
|
|
throw Error(
|
2011-03-16 17:50:11 -04:00
|
|
|
"Expecting at most two arguments for definition of named Class '" +
|
|
|
|
name + "'; " + arguments.length + " given."
|
2011-03-04 19:49:15 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2011-03-05 00:33:47 -05:00
|
|
|
// if no definition was given, return a staging object, to apply the name to
|
|
|
|
// the class once it is actually created
|
|
|
|
if ( def === undefined )
|
|
|
|
{
|
|
|
|
return createStaging( name );
|
|
|
|
}
|
2011-03-04 19:49:15 -05:00
|
|
|
// the definition must be an object
|
2011-03-05 00:33:47 -05:00
|
|
|
else if ( typeof def !== 'object' )
|
2011-03-04 19:49:15 -05:00
|
|
|
{
|
|
|
|
throw TypeError(
|
2011-03-05 12:56:14 -05:00
|
|
|
"Unexpected value for definition of named Class '" + name +
|
|
|
|
"'; object expected"
|
2011-03-04 19:49:15 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2011-03-04 23:44:19 -05:00
|
|
|
// add the name to the definition
|
|
|
|
def.__name = name;
|
|
|
|
|
2011-03-04 19:49:15 -05:00
|
|
|
return extend( def );
|
|
|
|
}
|
2010-11-14 22:07:04 -05:00
|
|
|
|
|
|
|
|
2011-03-05 00:33:47 -05:00
|
|
|
/**
|
|
|
|
* Creates a staging object to stage a class name
|
|
|
|
*
|
|
|
|
* The class name will be applied to the class generated by operations performed
|
|
|
|
* on the staging object. This allows applying names to classes that need to be
|
|
|
|
* extended or need to implement interfaces.
|
|
|
|
*
|
|
|
|
* @param {string} cname desired class name
|
|
|
|
*
|
|
|
|
* @return {Object} object staging the given class name
|
|
|
|
*/
|
|
|
|
function createStaging( cname )
|
|
|
|
{
|
|
|
|
return {
|
|
|
|
extend: function()
|
|
|
|
{
|
|
|
|
var args = Array.prototype.slice.apply( arguments );
|
|
|
|
|
|
|
|
// extend() takes a maximum of two arguments. If only one
|
|
|
|
// argument is provided, then it is to be the class definition.
|
|
|
|
// Otherwise, the first argument is the supertype and the second
|
|
|
|
// argument is the class definition. Either way you look at it,
|
|
|
|
// the class definition is always the final argument.
|
|
|
|
//
|
|
|
|
// We want to add the name to the definition.
|
|
|
|
args[ args.length - 1 ].__name = cname;
|
|
|
|
|
|
|
|
return extend.apply( null, args );
|
|
|
|
},
|
2011-03-05 03:22:45 -05:00
|
|
|
|
|
|
|
implement: function()
|
|
|
|
{
|
|
|
|
// implement on empty base, providing the class name to be used once
|
|
|
|
// extended
|
|
|
|
return createImplement(
|
2011-03-16 18:18:33 -04:00
|
|
|
null,
|
2011-03-05 03:22:45 -05:00
|
|
|
Array.prototype.slice.call( arguments ),
|
|
|
|
cname
|
|
|
|
);
|
|
|
|
},
|
2011-03-05 00:33:47 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-03-05 02:59:21 -05:00
|
|
|
/**
|
|
|
|
* Creates an intermediate object to permit implementing interfaces
|
|
|
|
*
|
|
|
|
* This object defers processing until extend() is called. This intermediate
|
|
|
|
* object ensures that a usable class is not generated until after extend() is
|
|
|
|
* called, as it does not make sense to create a class without any
|
|
|
|
* body/definition.
|
|
|
|
*
|
2011-03-16 18:18:33 -04:00
|
|
|
* @param {Object} base base class to implement atop of, or null
|
2011-03-05 03:22:45 -05:00
|
|
|
* @param {Array} ifaces interfaces to implement
|
|
|
|
* @param {string=} cname optional class name once extended
|
2011-03-05 02:59:21 -05:00
|
|
|
*
|
|
|
|
* @return {Object} intermediate implementation object
|
|
|
|
*/
|
2011-03-05 03:22:45 -05:00
|
|
|
function createImplement( base, ifaces, cname )
|
2011-03-05 02:59:21 -05:00
|
|
|
{
|
|
|
|
// Defer processing until after extend(). This also ensures that implement()
|
|
|
|
// returns nothing usable.
|
|
|
|
return {
|
2011-03-16 18:18:33 -04:00
|
|
|
extend: function()
|
2011-03-05 02:59:21 -05:00
|
|
|
{
|
2011-03-16 18:18:33 -04:00
|
|
|
var args = Array.prototype.slice.call( arguments ),
|
|
|
|
def = args.pop(),
|
|
|
|
ext_base = args.pop()
|
|
|
|
;
|
|
|
|
|
2011-03-16 19:50:47 -04:00
|
|
|
// if any arguments remain, then they likely misunderstood what this
|
|
|
|
// method does
|
|
|
|
if ( args.length > 0 )
|
|
|
|
{
|
|
|
|
throw Error(
|
|
|
|
"Expecting no more than two arguments for extend()"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2011-03-16 19:06:16 -04:00
|
|
|
// if a base was already provided for extending, don't allow them to
|
|
|
|
// give us yet another one (doesn't make sense)
|
|
|
|
if ( base && ext_base )
|
|
|
|
{
|
|
|
|
throw Error(
|
|
|
|
"Cannot override parent " + base.toString() + " with " +
|
|
|
|
ext_base.toString() + " via extend()"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2011-03-05 03:22:45 -05:00
|
|
|
// if a name was provided, use it
|
|
|
|
if ( cname )
|
|
|
|
{
|
|
|
|
def.__name = cname;
|
|
|
|
}
|
|
|
|
|
2011-03-16 18:18:33 -04:00
|
|
|
// If a base was provided when createImplement() was called, use
|
|
|
|
// that. Otherwise, use the extend() base passed to this function.
|
|
|
|
// If neither of those are available, extend from an empty class.
|
|
|
|
ifaces.push( base || ext_base || extend( {} ) );
|
|
|
|
|
2011-03-05 02:59:21 -05:00
|
|
|
return extend.apply( null, [
|
|
|
|
implement.apply( this, ifaces ),
|
|
|
|
def
|
|
|
|
] );
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
2010-11-14 22:07:04 -05:00
|
|
|
|
|
|
|
|
2010-11-15 06:54:37 -05:00
|
|
|
/**
|
2011-03-27 23:04:40 -04:00
|
|
|
* Mimics class inheritance
|
2010-11-15 06:54:37 -05:00
|
|
|
*
|
2011-03-27 23:04:40 -04:00
|
|
|
* 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.
|
2010-11-15 06:54:37 -05:00
|
|
|
*
|
2011-03-27 23:04:40 -04:00
|
|
|
* The class to inherit from (the first argument) is optional. If omitted, the
|
|
|
|
* first argument will be considered to be the properties list.
|
2010-11-15 06:54:37 -05:00
|
|
|
*
|
2011-03-27 23:04:40 -04:00
|
|
|
* @return {Object} extended class
|
2010-11-15 06:54:37 -05:00
|
|
|
*/
|
2011-03-27 23:04:40 -04:00
|
|
|
function extend()
|
2010-11-15 06:54:37 -05:00
|
|
|
{
|
2011-03-27 23:04:40 -04:00
|
|
|
// set up the new class
|
|
|
|
var data = class_builder.build.apply( null, arguments ),
|
2010-11-15 07:07:49 -05:00
|
|
|
|
2011-03-27 23:04:40 -04:00
|
|
|
new_class = data[ 'class' ],
|
|
|
|
abstract_methods = data.abstractMethods,
|
|
|
|
class_id = data.classId
|
|
|
|
;
|
2010-11-15 07:07:49 -05:00
|
|
|
|
2011-03-27 23:04:40 -04:00
|
|
|
// important: call after setting prototype
|
|
|
|
setupProps( new_class, abstract_methods, class_id );
|
2010-12-29 09:43:00 -05:00
|
|
|
|
2011-03-27 23:04:40 -04:00
|
|
|
// lock down the new class (if supported) to ensure that we can't add
|
|
|
|
// members at runtime
|
|
|
|
util.freeze( new_class );
|
2011-01-17 20:20:39 -05:00
|
|
|
|
2011-03-27 23:04:40 -04:00
|
|
|
return new_class;
|
|
|
|
}
|
2010-11-15 06:54:37 -05:00
|
|
|
|
|
|
|
|
2011-01-10 19:00:26 -05:00
|
|
|
/**
|
|
|
|
* Implements interface(s) into an object
|
|
|
|
*
|
2011-01-10 19:56:09 -05:00
|
|
|
* This will copy all of the abstract methods from the interface and merge it
|
|
|
|
* into the given object.
|
2011-01-10 19:00:26 -05:00
|
|
|
*
|
2011-01-10 19:56:09 -05:00
|
|
|
* @param {Object} base base object
|
2011-01-10 19:00:26 -05:00
|
|
|
* @param {...Interface} interfaces interfaces to implement into dest
|
|
|
|
*
|
|
|
|
* @return {Object} destination object with interfaces implemented
|
|
|
|
*/
|
|
|
|
var implement = function()
|
|
|
|
{
|
|
|
|
var args = Array.prototype.slice.call( arguments ),
|
2011-01-10 19:56:09 -05:00
|
|
|
dest = {},
|
2011-03-05 03:03:50 -05:00
|
|
|
base = args.pop(),
|
2011-01-10 19:00:26 -05:00
|
|
|
len = args.length,
|
|
|
|
arg = null,
|
|
|
|
|
|
|
|
abstract_list = [],
|
|
|
|
implemented = [];
|
|
|
|
|
|
|
|
// add each of the interfaces
|
|
|
|
for ( var i = 0; i < len; i++ )
|
|
|
|
{
|
|
|
|
arg = args[ i ];
|
|
|
|
|
|
|
|
// copy all interface methods to the class (does not yet deep copy)
|
2011-01-24 23:38:27 -05:00
|
|
|
util.propParse( arg.prototype, {
|
|
|
|
method: function( name, func, is_abstract, keywords )
|
|
|
|
{
|
|
|
|
dest[ name ] = func;
|
|
|
|
},
|
|
|
|
} );
|
2011-01-10 19:00:26 -05:00
|
|
|
implemented.push( arg );
|
|
|
|
}
|
|
|
|
|
2011-01-10 19:56:09 -05:00
|
|
|
// create a new class with the implemented abstract methods
|
2011-03-03 19:43:20 -05:00
|
|
|
var class_new = module.exports.extend( base, dest );
|
2011-03-27 23:04:40 -04:00
|
|
|
class_builder.getMeta( class_new ).implemented = implemented;
|
2011-01-10 19:00:26 -05:00
|
|
|
|
|
|
|
return class_new;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-10 23:29:28 -05:00
|
|
|
/**
|
2010-11-14 00:49:52 -05:00
|
|
|
* Sets up common properties for the provided function (class)
|
2010-11-10 23:29:28 -05:00
|
|
|
*
|
2011-03-03 22:33:18 -05:00
|
|
|
* @param {function()} func function (class) to set up
|
2010-12-19 13:54:31 -05:00
|
|
|
* @param {Array.<string>} abstract_methods list of abstract method names
|
2011-01-04 00:37:54 -05:00
|
|
|
* @param {number} class_id unique id to assign to class
|
2010-11-10 23:29:28 -05:00
|
|
|
*
|
2010-11-14 00:49:52 -05:00
|
|
|
* @return {undefined}
|
|
|
|
*/
|
2011-01-04 00:37:54 -05:00
|
|
|
function setupProps( func, abstract_methods, class_id )
|
2010-11-14 00:49:52 -05:00
|
|
|
{
|
2010-12-28 22:08:30 -05:00
|
|
|
attachAbstract( func, abstract_methods );
|
|
|
|
attachExtend( func );
|
2011-01-10 19:56:09 -05:00
|
|
|
attachImplement( func );
|
2011-01-04 00:37:54 -05:00
|
|
|
attachId( func, class_id );
|
2010-11-14 00:49:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-14 01:10:55 -05:00
|
|
|
/**
|
|
|
|
* Attaches isAbstract() method to the class
|
|
|
|
*
|
|
|
|
* @param {Function} func function (class) to attach method to
|
|
|
|
* @param {Array} methods abstract method names
|
|
|
|
*
|
|
|
|
* @return {undefined}
|
|
|
|
*/
|
2010-12-28 22:08:30 -05:00
|
|
|
function attachAbstract( func, methods )
|
2010-11-14 01:10:55 -05:00
|
|
|
{
|
2011-01-25 00:13:47 -05:00
|
|
|
var is_abstract = ( methods.__length > 0 ) ? true: false;
|
2010-11-14 01:10:55 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns whether the class contains abstract methods (and is therefore
|
|
|
|
* abstract)
|
|
|
|
*
|
|
|
|
* @return {Boolean} true if class is abstract, otherwise false
|
|
|
|
*/
|
2010-12-01 20:38:50 -05:00
|
|
|
util.defineSecureProp( func, 'isAbstract', function()
|
2010-11-14 01:10:55 -05:00
|
|
|
{
|
|
|
|
return is_abstract;
|
2010-11-14 20:31:23 -05:00
|
|
|
});
|
2010-11-14 01:10:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-14 00:49:52 -05:00
|
|
|
/**
|
|
|
|
* Attaches extend method to the given function (class)
|
|
|
|
*
|
|
|
|
* @param {Function} func function (class) to attach method to
|
|
|
|
*
|
|
|
|
* @return {undefined}
|
2010-11-10 23:29:28 -05:00
|
|
|
*/
|
2010-12-28 22:08:30 -05:00
|
|
|
function attachExtend( func )
|
2010-11-10 17:41:12 -05:00
|
|
|
{
|
2010-11-10 21:10:31 -05:00
|
|
|
/**
|
|
|
|
* Shorthand for extending classes
|
|
|
|
*
|
2010-12-01 23:19:59 -05:00
|
|
|
* This method can be invoked on the object, rather than having to call
|
2010-11-10 21:10:31 -05:00
|
|
|
* Class.extend( this ).
|
|
|
|
*
|
|
|
|
* @param {Object} props properties to add to extended class
|
|
|
|
*
|
|
|
|
* @return {Object} extended class
|
|
|
|
*/
|
2010-12-01 20:38:50 -05:00
|
|
|
util.defineSecureProp( func, 'extend', function( props )
|
2010-11-10 23:28:20 -05:00
|
|
|
{
|
|
|
|
return extend( this, props );
|
2010-11-14 22:07:04 -05:00
|
|
|
});
|
2010-11-10 21:10:31 -05:00
|
|
|
}
|
2010-11-14 21:50:56 -05:00
|
|
|
|
2010-12-29 21:32:16 -05:00
|
|
|
|
2011-01-10 19:56:09 -05:00
|
|
|
/**
|
|
|
|
* Attaches implement method to the given function (class)
|
|
|
|
*
|
2011-03-05 02:59:21 -05:00
|
|
|
* Please see the implement() export of this module for more information.
|
|
|
|
*
|
2011-01-10 19:56:09 -05:00
|
|
|
* @param {function()} func function (class) to attach method to
|
|
|
|
*
|
|
|
|
* @return {undefined}
|
|
|
|
*/
|
|
|
|
function attachImplement( func )
|
|
|
|
{
|
|
|
|
util.defineSecureProp( func, 'implement', function()
|
|
|
|
{
|
2011-03-05 02:59:21 -05:00
|
|
|
return createImplement(
|
|
|
|
func,
|
|
|
|
Array.prototype.slice.call( arguments )
|
|
|
|
);
|
2011-01-10 19:56:09 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-01-04 00:37:54 -05:00
|
|
|
/**
|
|
|
|
* Attaches the unique id to the class and its prototype
|
|
|
|
*
|
|
|
|
* The unique identifier is used internally to match a class and its instances
|
|
|
|
* with the class metadata. Exposing the id breaks encapsulation to a degree,
|
|
|
|
* but is a lesser evil when compared to exposing all metadata.
|
|
|
|
*
|
|
|
|
* @param {Function} func function (class) to attach method to
|
|
|
|
* @param {number} id id to assign
|
|
|
|
*
|
|
|
|
* @return {undefined}
|
|
|
|
*/
|
|
|
|
function attachId( func, id )
|
|
|
|
{
|
|
|
|
util.defineSecureProp( func, '__cid', id );
|
|
|
|
util.defineSecureProp( func.prototype, '__cid', id );
|
|
|
|
}
|
|
|
|
|