new Enumeration()
Subclasses of this class represent enumerated types. Instances of those
subclasses represent possible values of those types. Enumerated type
subclasses and their possible values can be defined as in the following:
exports.Suit = Enumeration.specialize("id", "name", {
SPADES: [0, "Spades"],
HEARTS: [1, "Hearts"],
DIAMONDS: [2, "Diamonds"],
CLUBS: [3, "Clubs"]
});
// To be use like this:
myCard = {value: 12, suit: Suit.HEARTS};
Enumerated types can also be defined as class properties using the
getterFor() constructor, as in the following:
exports.Card = Montage.specialize({
value: {
value: undefined
},
suit: {
value: undefined
}
}, {
Suit: {
get: Enumeration.getterFor("_Suit", "id", "name", {
SPADES: [1, "Spade"],
HEARTS: [2, "Heart"],
DIAMONDS: [3, "Diamond"],
CLUBS: [4, "Club"]
})
}
});
// To be use like this:
myCard = new Card();
myCard.value = 12;
myCard.suit = Card.Suit.HEARTS;
In addition to being created as shown above, instances of an enumerated
type subclasses can be created using a `with*()` class method that will be
automatically generated for each subclass based on the subclass' property
names, as in the following:
Card.Suit.ROSES = Card.Suit.withIdAndName(5, "Roses");
Once instances of an enumerated type subclass are created they can be looked
up using `for*()` class methods that will be automatically generated for each
of the subclass's unique property names, as in the following:
myCard.value = Math.floor(1 + 13 * Math.random());
myCard.suit = Card.Suit.forId(Math.floor(1 + 4 * Math.random()));
- Source:
- To Do:
-
- [Charles] Simplify this extremely, notably by making enumerations instances of the Enumeration class instead of subclasses of it and by reworking the getterFor() constructor.
- [Charles] Switch to initial-caps enumeration value names like "Spades" instead of all-caps names like "NAMES".
- [Charles] Provide a "values" property to the enumeration similar to Java's "values()" Enum method.
Extends
- external:Montage
Methods
(static) getterFor(key, uniquePropertyNamesopt, otherPropertyNamesopt, prototypeDescriptoropt, constructorDescriptoropt, constantsopt) → {function}
Creates and returns a getter which, when first called, will create and
cache a new enumeration subclass with the specified attributes.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
key |
string | ||
uniquePropertyNames |
Array.<string> | string |
<optional> |
|
otherPropertyNames |
Array.<string> | string |
<optional> |
|
prototypeDescriptor |
Object |
<optional> |
|
constructorDescriptor |
Object |
<optional> |
|
constants |
Object |
<optional> |
- Source:
- To Do:
-
- [Charles] Simplify this API, replacing it with simpler methods like Enumeration.withNames(), Enumeration.withValues(), and Enumeration.withPrototypeAndValues().
Returns:
- A getter that will create and cache the desired
Enumeration subclass.
- Type
- function
(static) specialize(uniquePropertyNamesopt, otherPropertyNamesopt, prototypeDescriptoropt, constructorDescriptoropt, constantsopt) → {function}
Creates a new enumeration subclass with the specified attributes.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
uniquePropertyNames |
Array.<string> | string |
<optional> |
|
otherPropertyNames |
Array.<string> | string |
<optional> |
|
prototypeDescriptor |
Object |
<optional> |
|
constructorDescriptor |
Object |
<optional> |
|
constants |
Object |
<optional> |
- Source:
- To Do:
-
- [Charles] Simplify this API, possibly by making "uniquePropertyNames", "otherPropertyNames", and "constants" properties of the derived instance, leaving "specialize()" to its "Montage.specialize()" API and meaning.
Returns:
- The created Enumeration subclass.
- Type
- function