By 06/07/2015 2 Comments

Static Functions & Properties In TypeScript

Definition Of Static Functions And Properties:

  • Like non-static functions or properties, a class can also contains static functions/methods and properties in TypeScript.
  • Static functions are naturally global.
  • Static functions and properties can be created by mark a keyword “static” as prefix.
  • Static functions and properties are not associated with a particular object. These are directly associated with a class itself and are free.
  • Static functions and properties can only be called through their class reference (by prefixing the class name with a function or a property name) and cannot be called through an object reference.
  • Static members are independent from any specific object.


class Calculator { // A class declaration

/* Following are two number type private properties which are marked as “static”. */
    private static _V1: number;
    private static _V2: number;

/* Following are “static” getter and setter accessors which are setting and returning two digit values. */
    static get v1(): number {
        return Calculator._V1;

     static set v1(value: number) {
         Calculator._V1 = value;

     static get v2(): number {
         return Calculator._V2;

     static set v2(value: number) {
         Calculator._V2 = value;

/* Following are number return type functions. These functions are declared as “static” functions and perform basic calculator operations.*/
     static add(): number {

         return Calculator.v1 + Calculator.v2;

     static substract(): number {

         return Calculator.v1 - Calculator.v2;

     static multiply(): number {

         return Calculator.v1 * Calculator.v2;

     static divide(): number {

         return Calculator.v1 / Calculator.v2;

/* Following statements call two static “set” accessors v1 and v2 and set different values to private properties for calculations. Notice: these two property “set” accessors are called through a class name (Calculator) reference. */
Calculator.v1 = 10;
Calculator.v2 = 5;

/* Following, property’s “get” accessors and different calculation functions are being called through a class name reference and also display different results. */
document.write("Addition: "+ Calculator.v1 + " + " + Calculator.v2 + " = " +Calculator.add() + "<br/>");
document.write("Substraction: " + Calculator.v1 + " - " + Calculator.v2 + " = " + Calculator.substract() + "<br/>");
document.write("Multiplication: " + Calculator.v1 + " * " + Calculator.v2 + " = " + Calculator.multiply() + "<br/>");
document.write("Division: " + Calculator.v1 + " / " + Calculator.v2 + " = " + Calculator.divide());

Some More Points About Static Functions And Properties:

  • Non-static functions and properties cannot be accessed/called within static functions but static functions and properties can be accessed/called within non-static functions through class name reference.
  • In TypeScript a class cannot be marked with “static” keyword.

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 .