1
0
Fork 0

No need to recheck the type each time

closure/master
Mike Gerwitz 2011-03-04 00:24:42 -05:00
parent 009c4a93e9
commit 7bb87e370f
2 changed files with 77 additions and 64 deletions

View File

@ -51,16 +51,19 @@ var class_meta = {};
module.exports = function() module.exports = function()
{ {
var def = {}, var def = {},
name = ''; name = '',
type = typeof arguments[ 0 ]
;
// anonymous class switch ( type )
if ( typeof arguments[ 0 ] === 'object' )
{ {
// anonymous class
case 'object':
def = arguments[ 0 ]; def = arguments[ 0 ];
// ensure we have the proper number of arguments (if they passed in too // ensure we have the proper number of arguments (if they passed in
// many, it may signify that they don't know what they're doing, and likely // too many, it may signify that they don't know what they're doing,
// they're not getting the result they're looking for) // and likely they're not getting the result they're looking for)
if ( arguments.length > 1 ) if ( arguments.length > 1 )
{ {
throw Error( throw Error(
@ -68,10 +71,11 @@ module.exports = function()
arguments.length + " given." arguments.length + " given."
); );
} }
}
break;
// named class // named class
else if ( typeof arguments[ 0 ] === 'string' ) case 'string':
{
name = arguments[ 0 ]; name = arguments[ 0 ];
def = arguments[ 1 ]; def = arguments[ 1 ];
@ -81,11 +85,14 @@ module.exports = function()
// the definition must be an object // the definition must be an object
if ( typeof def !== 'object' ) if ( typeof def !== 'object' )
{ {
throw TypeError( "Unexpected value for named class definition" ); throw TypeError(
"Unexpected value for named class definition"
);
} }
}
else break;
{
default:
// we don't know what to do! // we don't know what to do!
throw TypeError( throw TypeError(
"Expecting anonymous class definition or named class definition" "Expecting anonymous class definition or named class definition"

View File

@ -43,16 +43,19 @@ var util = require( './util' ),
module.exports = function() module.exports = function()
{ {
var def = {}, var def = {},
name = ''; name = '',
type = typeof arguments[ 0 ]
;
// anonymous interface switch ( type )
if ( typeof arguments[ 0 ] === 'object' )
{ {
// anonymous interface
case 'object':
def = arguments[ 0 ]; def = arguments[ 0 ];
// ensure we have the proper number of arguments (if they passed in too // ensure we have the proper number of arguments (if they passed in
// many, it may signify that they don't know what they're doing, and likely // too many, it may signify that they don't know what they're doing,
// they're not getting the result they're looking for) // and likely they're not getting the result they're looking for)
if ( arguments.length > 1 ) if ( arguments.length > 1 )
{ {
throw Error( throw Error(
@ -60,21 +63,24 @@ module.exports = function()
arguments.length + " given." arguments.length + " given."
); );
} }
}
break;
// named class // named class
else if ( typeof arguments[ 0 ] === 'string' ) case 'string':
{
name = arguments[ 0 ]; name = arguments[ 0 ];
def = arguments[ 1 ]; def = arguments[ 1 ];
// add the name to the definition // add the name to the definition
def.__name = name; def.__name = name;
}
else break;
{
default:
// we don't know what to do! // we don't know what to do!
throw TypeError( throw TypeError(
"Expecting anonymous interface definition or named interface definition" "Expecting anonymous interface definition or named " +
"interface definition"
); );
} }