Javascript 中模拟 Enum By shawl.qiu
说明:
这个模拟起来挻简单的, 不过由于 JS 程序一般都很小, 基本上不需要使用像Enum 这样的数据类型...
相关文章见: shawl.qiu Javascript 哈希表类 / HashTable v1.0
http://blog.csdn.net/btbtd/archive/2007/06/28/1669636.aspx
shawl.qiu
2007-08-22
http://blog.csdn.net/btbtd
内容:
// shawl.qiu JavaScript Document
/*-----------------------------------------------------------------------------------*/
* shawl.qiu Enum class v1.0
/*-----------------------------------------------------------------------------------*/
//---------------------------------begin class Enum()-------------------------------//
function Enum ( )
{ // shawl.qiu code : return object
//------------------------------------begin private variable
var Arg = arguments;
//------------------------------------begin private method
function fGetEnum ( )
{
var This = this;
var ArgLen = Arg. length;
var Index = 0;
if ( ArgLen > 0 )
{
for ( var i =0; i <ArgLen; i ++ )
{
Arg [ i ] += "";
Arg [ i ] = Arg [ i ]. replace ( / /s+ / g, "" );
if ( Arg [ i ]. indexOf ( "=" ) > - 1 )
{
var TempAr = Arg [ i ]. split ( "=" );
if ( TempAr. length != 2 )
{
throw new Error ( ["枚举类型: ", Arg [ i ], " 初始化出错!" ]. join ( '' ) );
}
var iTemp = parseInt ( TempAr [ 1 ] );
if ( isNaN ( iTemp ) )
{
throw new Error ( ["枚举类型: ", Arg [ i ], " 初始值出错!" ]. join ( '' ) );
}
This [ TempAr [ 0 ] ] = iTemp;
Index = ++ iTemp;
}
else
{
This [ Arg [ i ] ] = Index ++;
}
}
}
else
{
fFatalError ( "枚举类型必须有初始参数!" );
}
return This;
}
function fFatalError ( sMsg )
{ // shawl.qiu code : void return
if (! sMsg ) sMsg = "发生严重错误, 现在终止程序运行!";
throw new Error ( sMsg );
} // end function fFatalError
//------------------------------------end private method
//------------------------------------begin initer
return new fGetEnum ( );
//------------------------------------end initer
} // shawl.qiu code
//---------------------------------end class Enum()---------------------------------//
/*-----------------------------------------------------------------------------------*/
* shawl.qiu Enum class v1.0
/*-----------------------------------------------------------------------------------*/
//---------------------------------begin class Enum()-------------------------------//
function Enum ( )
{ // shawl.qiu code : return object
//------------------------------------begin private variable
var Arg = arguments;
//------------------------------------begin private method
function fGetEnum ( )
{
var This = this;
var ArgLen = Arg. length;
var Index = 0;
if ( ArgLen > 0 )
{
for ( var i =0; i <ArgLen; i ++ )
{
Arg [ i ] += "";
Arg [ i ] = Arg [ i ]. replace ( / /s+ / g, "" );
if ( Arg [ i ]. indexOf ( "=" ) > - 1 )
{
var TempAr = Arg [ i ]. split ( "=" );
if ( TempAr. length != 2 )
{
throw new Error ( ["枚举类型: ", Arg [ i ], " 初始化出错!" ]. join ( '' ) );
}
var iTemp = parseInt ( TempAr [ 1 ] );
if ( isNaN ( iTemp ) )
{
throw new Error ( ["枚举类型: ", Arg [ i ], " 初始值出错!" ]. join ( '' ) );
}
This [ TempAr [ 0 ] ] = iTemp;
Index = ++ iTemp;
}
else
{
This [ Arg [ i ] ] = Index ++;
}
}
}
else
{
fFatalError ( "枚举类型必须有初始参数!" );
}
return This;
}
function fFatalError ( sMsg )
{ // shawl.qiu code : void return
if (! sMsg ) sMsg = "发生严重错误, 现在终止程序运行!";
throw new Error ( sMsg );
} // end function fFatalError
//------------------------------------end private method
//------------------------------------begin initer
return new fGetEnum ( );
//------------------------------------end initer
} // shawl.qiu code
//---------------------------------end class Enum()---------------------------------//