Simulate Enum by shawl. Qiu in Javascript
Note:
This simulation is simple, but because JS programs are usually very small, basically no data type like Enum is needed...
For more information, see shawl. Qiu JavaScript hash table/hashtable V1.0.
Http://blog.csdn.net/btbtd/archive/2007/06/28/1669636.aspx
Shawl. Qiu
2007-08-22
Http://blog.csdn.net/btbtd
Content: // shawl. Qiu JavaScript document
/* Optional /*-----------------------------------------------------------------------------------*/
* Shawl. Qiu Enum class V1.0
/* Optional /*-----------------------------------------------------------------------------------*/
// ----------------------------------- 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 (["Enumeration type:", Arg [I], "initialization error! "]. Join (''));
}
VaR itemp = parseint (tempar [1]);
If (isnan (itemp ))
{
Throw new error (["Enumeration type:", Arg [I], "initial value error! "]. Join (''));
}
This [tempar [0] = itemp;
Index = ++ itemp;
}
Else
{
This [Arg [I] = index ++;
}
}
}
Else
{
Ffatalerror ("the enumeration type must have an initial parameter! ");
}
Return this;
}
Function ffatalerror (smsg)
{// Shawl. Qiu code: void return
If (! Smsg) smsg = "a serious error occurs. Terminate the program now! ";
Throw new error (smsg );
} // End function ffatalerror
// -------------------------------------- End private Method
// -------------------------------------- Begin initer
Return new fgetenum ();
// -------------------------------------- End initer
} // Shawl. Qiu code
// ----------------------------------- End class Enum ()---------------------------------//