By 06/07/2015 16 Comments

Enumeration In TypeScript

Enumeration:

  • In TypeScript an enumeration is a collection/list of numeric value constants which has different meaningful human readable names/identifiers and it is created by a keyword “enum”.
  • An enumeration is a way to define an easily remembered and human readable names which are represented by special numbers/numeric values.
  • Each name represents a numeric type value. The names defined in an enumeration are called symbols or identifiers which are separated by comma and called an enumeration-list.
  • An enumeration can be used as a control variable in “switch” and loops.
  • Like an array, symbols / names in an enumeration starts from 0 by default, it means the first enumeration symbol / name has value 0 and so on. We can also assign custom numeric values manually into one or more enum names through an assignment (=) operator in enumeration-list. These custom numeric values will override the default values of enums.
  • Enumerations are depended on “numeric” type by default.
  • Each enumeration member can be accessed through its name by dot (.) operator.
  • Enumeration plays an important role in real world programs. It saves our time, gives a clear understand of code to programmers, it sends user command options to an application in meaningful string names such as Player.play, Player.stop etc. instead of 0 for “play”, 1 for “stop” etc. An enum makes our code more readable and more maintainable.
  • In TypeScript an enumeration cannot be declared inside a class.

Example – 1, following both examples display numeric values of each enum named constants:

// An enum declaration with name “PlayControls”, this enum has different names.
enum PlayControls {
    Play,    // Position 0
    Pause,   // Position 1 
    Stop,    // Position 2
    Prev,    // Position 3
    Next     // Position 4
}

/* function declaration which returns entire enum. This function has implicitly an enum as its return type. */
function getEnums() {
    return PlayControls;
}

/* Above function is called and assigned to a local variable, now this local variable has become an enum type and can access the enum values */
var controls = getEnums();

/* Each enum numeric value named constant is accessed through an enum type variable and also printed. */
document.write("Play controls is: " + controls.Play + "<br/>"); // 0
document.write("Pause controls is: " + controls.Pause + "<br/>");//1 
document.write("Stop controls is: " + controls.Stop + "<br/>");// 2
document.write("Prev controls is: " + controls.Prev + "<br/>");// 3
document.write("Next controls is: " + controls.Next);  // 4

Example-2:

// An enum declaration with name “PlayControls”, this enum has different names.
enum PlayControls {
    Play,   // Position 0
    Pause,  // Position 1
    Stop,   // Position 2
    Prev,   // Position 3
    Next    // Position 4
}

/* function declaration which returns entire enum. This function has implicitly an enum as its return type. */
function getEnums() {
    return PlayControls;
}

/* Above function is called and assigned to a local variable, now this local variable has become an enum type and can access the enum values */
var controls = getEnums();

/* Each enum numeric value named constant is accessed through an enum type variable, string literals and also printed. */
document.write("Play controls is: " + controls["Play"] + "<br/>"); //0
document.write("Pause controls is: " + controls["Pause"]+ "<br/>");//1
document.write("Stop controls is: " + controls["Stop"] + "<br/>");//2
document.write("Prev controls is: " + controls["Prev"]+ "<br/>");//3
document.write("Next controls is: " + controls["Next"]); // 4

Example – 3, generated JavaScript .js file from above const enum example:

/* NOTE: JavaScript closure is generated from enum. */
var PlayControls;
(function (PlayControls) {  // Starting scope of JavaScript closure.

    PlayControls[PlayControls["Play"] = 0] = "Play";
    PlayControls[PlayControls["Pause"] = 1] = "Pause";
    PlayControls[PlayControls["Stop"] = 2] = "Stop";
    PlayControls[PlayControls["Prev"] = 3] = "Prev";
    PlayControls[PlayControls["Next"] = 4] = "Next";

}) // Ending scope of JavaScript closure.

(PlayControls || (PlayControls = {}));

// Generated print statements.
document.write("Play controls is: " + PlayControls["Play"] + "<br/>"); //0
document.write("Pause controls is: " + PlayControls["Pause"] + "<br/>"); //1
document.write("Stop controls is: " + PlayControls["Stop"] + "<br/>"); //2
document.write("Prev controls is: " + PlayControls["Prev"] + "<br/>"); //3
document.write("Next controls is: " + PlayControls["Next"]); // 4

Example – 4, enum human readable names/values are displayed:

// An enum declaration with name “PlayControls”, this enum has different names.
enum PlayControls {
    Play,       // Position 0
    Pause,		// Position 1
    Stop,		// Position 2
    Prev,		// Position 3
    Next		// Position 4
}

/* function declaration which returns entire enum. This function has implicitly an enum as its return type. */
function getEnums() {
    return PlayControls;
}

/* Above function is called and assigned to a local variable, now this local variable has become an enum type and can access the enum values */
var controls = getEnums();

/* For loop which is executed from 0 to 4 and displays each enum named values or human readable name. */
for (var count = 0; count < 5; count++)
    document.write("At position: " + count + " = " + controls[count] + "<br/>");

Example – 5, use of enum in real world scenario:

// An enum declaration with name “PlayControls”, this enum has different names.
enum PlayControls {
    Play,  // Position 0
    Pause, // Position 1
    Stop,  // Position 2
    Prev,  // Position 3
    Next   // Position 4
}

/* 1 parametrized (enum type) function declaration which has a switch statement inside it. This function has explicitly void as its return type. */
function playerCommand(controls: PlayControls): void { //function start

/* Switch statement declaration which tests the input conditions of enum types. */
    switch (controls) { // starting scope of switch

/* Multiple cases which compares the enum value with its switch expression. It any case will be matched with its switch that case’s body will be executed. */
        case PlayControls.Play: // 0
            document.write("Playing... <br/>");
            break;

        case PlayControls.Pause: // 1
            document.write("Paused. <br/>");
            break;

        case PlayControls.Stop: // 2
            document.write("Stop.<br/>");
            break;

        case PlayControls.Prev: // 3
            document.write("Prev...<br/>");
            break;

        case PlayControls.Next: // 4
            document.write("Next...<br/>");
            break;

/* If no case will be matched then the default statement will be executed.*/
        default:
            document.write("Invalid command!");
            break;

    } // Ending scope of switch
} // Ending of function.

/* Function is being called with different enum commands as its arguments.*/
playerCommand(PlayControls.Play);   // 0
playerCommand(PlayControls.Pause);  // 1
playerCommand(PlayControls.Stop);   // 2
playerCommand(PlayControls.Prev);   // 3
playerCommand(PlayControls.Next);   // 4

Example – 6:, assigning custom numeric values manually to enum numeric named constants:

/* An enum declaration with name “PlayControls”, this enum has different names. Different custom numeric values are being assigned in each named constants manually. */
enum PlayControls {
    Play = 2,   // Position 2
    Pause= 3,   // Position 3
    Stop = 0,   // Position 0
    Prev = 4,   // Position 4
    Next = 7    // Position 7
}

/* Each enum numeric value named constant is accessed through an enum type variable, string literals and also printed. */
document.write("Play controls is: " + PlayControls["Play"] + "<br/>"); //2
document.write("Pause controls is: " + PlayControls["Pause"] + "<br/>");//3
document.write("Stop controls is: " + PlayControls["Stop"] + "<br/>");//0
document.write("Prev controls is: " + PlayControls["Prev"] + "<br/>");//4
document.write("Next controls is: " + PlayControls["Next"]); // 7





Next Tutorial →


← Previous Tutorial


Leave your valuable Comment

Have a natural attraction for women cosmetics and replica watches uk clothes, no
more than two for men the most attractive one, is to make their own driving experience, happy and can serve as the facade of the car, another is to highlight the taste edify sentiment rolex replica watch. The replica rolex is undoubtedly the most fashionable accessories, wear a suit to attend the activities, but also get a decent match on the replica watches .