Skip to content

Latest commit

 

History

History
154 lines (113 loc) · 6.87 KB

enum.md

File metadata and controls

154 lines (113 loc) · 6.87 KB

Enum

This is the common base class for all enumeration types. Enum constants are immutable; once declared they can't be modified.

Enum types are iterable, that's to say, they inherit a [Symbol.iterator] method implemented by this class. Therefore, enum types may be used in "for of" blocks in order to iterate over its declared constants.

Kind: global class
Properties

Name Type Description
name string The name of the enum constant, exactly as declared in the enum declaration.
ordinal number An integer indicating the ordinal of this enumeration constant (its position in its enum declaration, where the initial constant is assigned a value of zero).
type string A string representing the type of this constant (actually, the name of the class that this constant extends from).
_ string An alias to Enum.toString. This property offers a short and convenient way of obtaining the string representation of this constant.

new Enum()

Enum types may not be instantiated through the new operator. The constructor guarantees such invariant by throwing an AssertionError whenever called.

Throws:

  • AssertionError Whenever that one uses the new operator with an enum type.

enum.isConstantOf(anEnum) ⇒ boolean

Returns true whether this enum constant bellongs to the specified enum class or false otherwise.

Kind: instance method of Enum
Returns: boolean - True or false indicating waht is explained above.

Param Type Description
anEnum Enum Any enum class.

enum.isSameTypeAs(that) ⇒ boolean

Returns true whether this constant bellongs to the same enum class of the specified constant or false otherwise.

Kind: instance method of Enum
Returns: boolean - True or false indicating what is explained above.

Param Type Description
that Enum Any enum constant.

enum.compareTo(that) ⇒ number

Compares this enum constant with the specified object for order (by using the ordinal property). Returns a negative integer, 0 or a positive integer if this enum constant is less than, equal to or greather than the specified parameter. Enum constants are only comparable to other enum constants of the same type. If a enum constant of a different type or even an arbitrary object is passed as a parameter, an error is thrown.

Kind: instance method of Enum
Returns: number - A negative integer, zero or a positive integer following the algorithm cited above.
Throws:

  • Error If the preconditions mentioned above aren't satisfied.
Param Type Description
that Enum An enum constant of the same type of this enum constant.

enum.toString() ⇒ string

Returns a string representing this enum constant. By default, this method returns the name of this constant (same as the property name). However, this method may be overriden in order to provide a more friendly string form of the enum constant.

Kind: instance method of Enum
Returns: string - String representation of this constant.

Enum.values(...constants)

Initializes this enum type, by creating the specified constant values. Each constant will inherit the prototype of this enum and will be added as immutable properties of the current enum subclass. Enum constants are immutable and and may not be configured. Further more, as a side efect of calling this method, the own enum become immutable and will no longer be possible to modify it.

Notice that isn't possible to call this method directly in the class Enum. Rather, it's necessary to create a subclass of Enum, which will act as the base for the new enum type. In addition, this method must be invoked once; any attempt of calling it on an initialized enum typ will result in an AssertionError.

For more details about how to use this method, check out the examples.

Kind: static method of Enum
Throws:

  • AssertionError If the invariants mentioned previously aren't respected.
Param Type Description
...constants string | object Rest parameters representing the enum declarations. If a string is specified it will be the name of the constant. If the enum declaration is an object, it must respect the following form: js {name: {property1: value1, property2: value2, propertyN: valueN}} In other words, each enum declaration must be a plain object with one and only one property (that will be the enum constant's name) and a set of members (attributes and/or methods) assigned to this property. Any violation to these invariants will cause an AssertionError.

Enum.valueOf() ⇒ Enum

Returns the enum constant whose the result of an invokation of the method Enum.toString matches the provided parameter name. The name must match exactly the return value of Enum.toString; extraneous whitespaces are not permitted.

Kind: static method of Enum
Returns: Enum - The enum constant whose #toString() matches the specified name.
Throws:

  • AssertionError If the provided name is not a string or if there is no an enum constant that corresponds to the specified name.

Enum.all() ⇒ array

Returns an array containing all declared constants of this enum type.

Kind: static method of Enum
Returns: array - Array with all constants that bellong to this enum.