Hi,
how can I create an enumeration in javascript?
Hi,
how can I create an enumeration in javascript?
Bottom line: You can't.
You can fake it, but you won't get type safety. Typically this is done by creating a simple dictionary of string values mapped to integer values. For example:
var DaysEnum = {"monday":1, "tuesday":2, "wednesday":3, ...}
Document.Write("Enumerant: " + DaysEnum.tuesday);
The problem with this approach? You can accidentally redefine your enumerant, or accidentally have duplicate enumerant values. For example:
DaysEnum.monday = 4; // whoops, monday is now thursday, too
This is the best solution I've found:
http://www.ditchnet.org/wp/2005/03/21/typesafe-enum-pattern-in-javascript/