By 06/07/2015 3 Comments

TypeScript Anonymous Functions

Anonymous Functions:

  • The TypeScript language has the concept of anonymous functions or methods.
  • An anonymous function has not a specific/particular name.
  • Anonymous functions are declared as unnamed or unknown functions.
  • An anonymous function is must be assigned to a variable at its declaration time.
  • Anonymous functions can be void or any other type.

Example:

/* An anonymous function declaration which has implicitly void as its return type and 1 string type parameter. This unknown function is assigned to a local variable. */
var calculatorType = function (name: string) {

    // print output
    document.write("Calculator type: " + name + " <br/>");
};

/* Multiple anonymous functions declaration, each function has explicitly void as its return type, 2 number type parameters and different name. Function name and its parameter-list are collectively called function signature. These functions are assigned to a local variable. */
var add = function (v1: number, v2: number): void {
    document.write("Addition: " + (v1 + v2) + " <br/>");
};

var substract = function (v1: number, v2: number): void {
    document.write("Substraction: " + (v1 - v2) + " <br/>");
};

var multiply = function (v1: number, v2: number): void {
    document.write("Multiplication: " + (v1 * v2) + " <br/>");
};

/* An anonymous function which has number as its return type and assigned to a local variable. */
var divide = function (v1: number, v2: number): number {
    return (v1 / v2);
};

/* Above functions are called. Each function is called through its name. Notice that, 2 numbers add(10, 2) are passed from each function, these numbers are called argument(s) and must be matched with the type of parameters, If they not matched the TypeScript compiler will generates a compile-time error. */
calculatorType("Normal calculator");
add(10,2)
substract(10, 2);
multiply(10, 2);
document.write("Division: " + divide(10, 2));






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 .