enum javascript
const seasons = {
SUMMER: {
BEGINNING: "summer.beginning",
ENDING: "summer.ending"
},
WINTER: "winter",
SPRING: "spring",
AUTUMN: "autumn"
};
let season = seasons.SUMMER.BEGINNING;
if (!season) {
throw new Error("Season is not defined");
}
switch (season) {
case seasons.SUMMER.BEGINNING:
// Do something for summer beginning
case seasons.SUMMER.ENDING:
// Do something for summer ending
case seasons.SUMMER:
// This will work if season = seasons.SUMMER
// Do something for summer (generic)
case seasons.WINTER:
//Do something for winter
case seasons.SPRING:
//Do something for spring
case seasons.AUTUMN:
//Do something for autumn
}enum AnEnum {
One = 1,
Two = 2
}
let stringOne = AnEnum[1]; // "One"
let stringTwo = AnEnum[AnEnum.Two]; // "Two"const DaysEnum = {"monday":1, "tuesday":2, "wednesday":3, ...}
Object.freeze(DaysEnum)
enum Response {
No = 0,
Yes = 1,
}
function respond(recipient: string, message: Response): void {
// ...
}
respond("Princess Caroline", Response.Yes)
enum Direction {
Up,
Down,
Left,
Right,
}
array.every(elm,index,array) // Did all elements satisfy the callback?
array.find(elm,index,array) // Find the first element that satisfies the callback
array.findIndex(elm,index,array) // Find the first element that satisfies the callback's index
array.map(elm,index,array) // Transform every element and create a new array
array.reduce(accumulator,elm,index,array) // Reduce every element into a new value
array.some(elm,index,array) // Did any element satisfy the callback?