Introduction

An enum type is a special variable that contains a set of predefined constants. When a variable is matched against the associated Enum, the data of the variable must exist in the Enum as the data of the predefined constant. Common examples include compass directions (NORTH, SOUTH, EAST, WEST) and the days of the week.

Constants must be written in uppercase. Constants cannot be changed once they are defined.

var Compass = Enum('Compass', {
    NORTH : 1,
    SOUTH : 2,
    EAST  : 3,
    WEST  : 4
});

console.log(Compass.NORTH) // 1
console.log(Compass.EAST)  // 3

When declaring an Enum-type, you need to give it a name in the first argument. This name can be used as a type when declaring properties. The argument given to the associated setter will validate if the Enum-type contains a constant of the given value.

For example:

var MyClass = Class({
    'public Compass direction': Compass.NORTH
});

var mc = new MyClass();

mc.setDirection(Compass.SOUTH); // OK
mc.setDirection('foobar'); // Error: setDirection: 'foobar' is not a member of enum Compass.

Predefined constants in an enum-type may only contain scalar values. It is not allowed to use functions and objects.

Read more about this in the validation section.