Initial implementation of natural class definition by invoking class module
parent
15ac62d063
commit
d23ae6210c
55
lib/class.js
55
lib/class.js
|
@ -35,6 +35,43 @@ var util = require( './util' ),
|
||||||
var class_meta = {};
|
var class_meta = {};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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.
|
||||||
|
*
|
||||||
|
* @param {Object} def class definition
|
||||||
|
*
|
||||||
|
* @return {Class} new class
|
||||||
|
*/
|
||||||
|
module.exports = function( def )
|
||||||
|
{
|
||||||
|
// the class definition should be an object
|
||||||
|
if ( typeof def !== 'object' )
|
||||||
|
{
|
||||||
|
throw TypeError(
|
||||||
|
"Must provide class definition when declaring a new class"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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 Class definition; " +
|
||||||
|
arguments.length + " given."
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return extend( def );
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a class, inheriting either from the provided base class or the
|
* Creates a class, inheriting either from the provided base class or the
|
||||||
* default base class
|
* default base class
|
||||||
|
@ -43,7 +80,7 @@ var class_meta = {};
|
||||||
*
|
*
|
||||||
* @return {Object} extended class
|
* @return {Object} extended class
|
||||||
*/
|
*/
|
||||||
exports.extend = function( base )
|
module.exports.extend = function( base )
|
||||||
{
|
{
|
||||||
return extend.apply( this, arguments );
|
return extend.apply( this, arguments );
|
||||||
};
|
};
|
||||||
|
@ -56,12 +93,12 @@ exports.extend = function( base )
|
||||||
*
|
*
|
||||||
* @return {Class} new class containing interface abstractions
|
* @return {Class} new class containing interface abstractions
|
||||||
*/
|
*/
|
||||||
exports.implement = function()
|
module.exports.implement = function()
|
||||||
{
|
{
|
||||||
var args = Array.prototype.slice.call( arguments );
|
var args = Array.prototype.slice.call( arguments );
|
||||||
|
|
||||||
// apply to an empty (new) object
|
// apply to an empty (new) object
|
||||||
args.unshift( exports.extend() );
|
args.unshift( module.exports.extend() );
|
||||||
|
|
||||||
return implement.apply( this, args );
|
return implement.apply( this, args );
|
||||||
};
|
};
|
||||||
|
@ -74,7 +111,7 @@ exports.implement = function()
|
||||||
*
|
*
|
||||||
* @return {boolean} true if class (created through ease.js), otherwise false
|
* @return {boolean} true if class (created through ease.js), otherwise false
|
||||||
*/
|
*/
|
||||||
exports.isClass = function( obj )
|
module.exports.isClass = function( obj )
|
||||||
{
|
{
|
||||||
obj = obj || {};
|
obj = obj || {};
|
||||||
|
|
||||||
|
@ -94,7 +131,7 @@ exports.isClass = function( obj )
|
||||||
* @return {boolean} true if instance of class (created through ease.js),
|
* @return {boolean} true if instance of class (created through ease.js),
|
||||||
* otherwise false
|
* otherwise false
|
||||||
*/
|
*/
|
||||||
exports.isClassInstance = function( obj )
|
module.exports.isClassInstance = function( obj )
|
||||||
{
|
{
|
||||||
obj = obj || {};
|
obj = obj || {};
|
||||||
|
|
||||||
|
@ -117,7 +154,7 @@ exports.isClassInstance = function( obj )
|
||||||
*
|
*
|
||||||
* @return {boolean} true if instance is an instance of type, otherwise false
|
* @return {boolean} true if instance is an instance of type, otherwise false
|
||||||
*/
|
*/
|
||||||
exports.isInstanceOf = function( type, instance )
|
module.exports.isInstanceOf = function( type, instance )
|
||||||
{
|
{
|
||||||
var meta, implemented, i;
|
var meta, implemented, i;
|
||||||
|
|
||||||
|
@ -162,7 +199,7 @@ exports.isInstanceOf = function( type, instance )
|
||||||
* accurately conveys the act of inheritance, implementing interfaces and
|
* accurately conveys the act of inheritance, implementing interfaces and
|
||||||
* traits, etc.
|
* traits, etc.
|
||||||
*/
|
*/
|
||||||
exports.isA = exports.isInstanceOf;
|
module.exports.isA = module.exports.isInstanceOf;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -429,7 +466,7 @@ var implement = function()
|
||||||
}
|
}
|
||||||
|
|
||||||
// create a new class with the implemented abstract methods
|
// create a new class with the implemented abstract methods
|
||||||
var class_new = exports.extend( base, dest );
|
var class_new = module.exports.extend( base, dest );
|
||||||
getMeta( class_new.__cid ).implemented = implemented;
|
getMeta( class_new.__cid ).implemented = implemented;
|
||||||
|
|
||||||
return class_new;
|
return class_new;
|
||||||
|
@ -594,7 +631,7 @@ function attachInstanceOf( instance )
|
||||||
{
|
{
|
||||||
var method = function( type )
|
var method = function( type )
|
||||||
{
|
{
|
||||||
return exports.isInstanceOf( type, instance );
|
return module.exports.isInstanceOf( type, instance );
|
||||||
};
|
};
|
||||||
|
|
||||||
util.defineSecureProp( instance, 'isInstanceOf', method );
|
util.defineSecureProp( instance, 'isInstanceOf', method );
|
||||||
|
|
|
@ -30,7 +30,25 @@ var foo_props = {
|
||||||
one: 1,
|
one: 1,
|
||||||
two: 2,
|
two: 2,
|
||||||
},
|
},
|
||||||
Foo = Class.extend( foo_props );
|
|
||||||
|
// there are two different means of extending; we want to test them both
|
||||||
|
classes = [
|
||||||
|
Class.extend( foo_props ),
|
||||||
|
Class( foo_props ),
|
||||||
|
],
|
||||||
|
|
||||||
|
class_count = classes.length
|
||||||
|
|
||||||
|
// will hold the class being tested
|
||||||
|
Foo = null
|
||||||
|
;
|
||||||
|
|
||||||
|
|
||||||
|
// Run all tests for both. This will ensure that, regardless of how the class is
|
||||||
|
// created, it operates as it should. Fortunately, these tests are fairly quick.
|
||||||
|
for ( var i = 0; i < class_count; i++ )
|
||||||
|
{
|
||||||
|
Foo = classes[ i ];
|
||||||
|
|
||||||
assert.ok(
|
assert.ok(
|
||||||
( Foo.extend instanceof Function ),
|
( Foo.extend instanceof Function ),
|
||||||
|
@ -223,6 +241,7 @@ assert.ok(
|
||||||
),
|
),
|
||||||
"Instances of subtypes do not share property references"
|
"Instances of subtypes do not share property references"
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// otherwise it'll output the internal constructor code, which is especially
|
// otherwise it'll output the internal constructor code, which is especially
|
||||||
|
@ -236,3 +255,41 @@ assert.ok(
|
||||||
);
|
);
|
||||||
} )();
|
} )();
|
||||||
|
|
||||||
|
|
||||||
|
( function testInvokingClassModuleRequiresObjectAsArgumentIfCreating()
|
||||||
|
{
|
||||||
|
assert.throws( function()
|
||||||
|
{
|
||||||
|
Class( 'moo' );
|
||||||
|
Class( 5 );
|
||||||
|
Class( false );
|
||||||
|
Class();
|
||||||
|
},
|
||||||
|
TypeError,
|
||||||
|
"Invoking class module requires object as argument if extending " +
|
||||||
|
"from base class"
|
||||||
|
);
|
||||||
|
|
||||||
|
var args = [ {}, 'one', 'two', 'three' ];
|
||||||
|
|
||||||
|
// we must only provide one argument if the first argument is an object (the
|
||||||
|
// class definition)
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Class.apply( null, args );
|
||||||
|
|
||||||
|
// if all goes well, we don't get to this line
|
||||||
|
assert.fail(
|
||||||
|
"Only one argument for class definitions is permitted"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
catch ( e )
|
||||||
|
{
|
||||||
|
assert.notEqual(
|
||||||
|
e.toString().match( args.length + ' given' ),
|
||||||
|
null,
|
||||||
|
"Class invocation should give argument count on error"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} )();
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue