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,45 +51,52 @@ var class_meta = {};
module.exports = function()
{
var def = {},
name = '';
name = '',
type = typeof arguments[ 0 ]
;
// anonymous class
if ( typeof arguments[ 0 ] === 'object' )
switch ( type )
{
def = arguments[ 0 ];
// anonymous class
case 'object':
def = arguments[ 0 ];
// 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."
// 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."
);
}
break;
// named class
case 'string':
name = arguments[ 0 ];
def = arguments[ 1 ];
// add the name to the definition
def.__name = name;
// the definition must be an object
if ( typeof def !== 'object' )
{
throw TypeError(
"Unexpected value for named class definition"
);
}
break;
default:
// we don't know what to do!
throw TypeError(
"Expecting anonymous class definition or named class definition"
);
}
}
// named class
else if ( typeof arguments[ 0 ] === 'string' )
{
name = arguments[ 0 ];
def = arguments[ 1 ];
// add the name to the definition
def.__name = name;
// the definition must be an object
if ( typeof def !== 'object' )
{
throw TypeError( "Unexpected value for named class definition" );
}
}
else
{
// we don't know what to do!
throw TypeError(
"Expecting anonymous class definition or named class definition"
);
}
return extend( def );

View File

@ -43,39 +43,45 @@ var util = require( './util' ),
module.exports = function()
{
var def = {},
name = '';
name = '',
type = typeof arguments[ 0 ]
;
// anonymous interface
if ( typeof arguments[ 0 ] === 'object' )
switch ( type )
{
def = arguments[ 0 ];
// anonymous interface
case 'object':
def = arguments[ 0 ];
// 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 Interface definition; " +
arguments.length + " given."
// 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 Interface definition; " +
arguments.length + " given."
);
}
break;
// named class
case 'string':
name = arguments[ 0 ];
def = arguments[ 1 ];
// add the name to the definition
def.__name = name;
break;
default:
// we don't know what to do!
throw TypeError(
"Expecting anonymous interface definition or named " +
"interface definition"
);
}
}
// named class
else if ( typeof arguments[ 0 ] === 'string' )
{
name = arguments[ 0 ];
def = arguments[ 1 ];
// add the name to the definition
def.__name = name;
}
else
{
// we don't know what to do!
throw TypeError(
"Expecting anonymous interface definition or named interface definition"
);
}
return extend( def );