By 06/07/2015 15 Comments

Property And Variable Scope In TypeScript

Property And Variable Scope Definition:

  • The TypeScript scope of a variable or a property is an area or region of code, which indicates that where these variables is being accessed from that area or region.
  • The valid scopes of properties or variables can be defined through TypeScript’s lexical scoping rules.
  • The lexical scoping rule means to declare any property/variable in a particular scope body/location such as: a variable/property can be declared in a class scope body or in a function scope body.
  • Child/nested functions can access to the variables that are declared/defied in their parent scope body but parent functions cannot access to the variable that are declared/defined in their child/nested scope body.
  • Nested scope is also called an inner scope and parent scope is called an outer scope.


// Following is an outer or parent function declaration.
function displayList() { // Outer/parent function scope starts.

/* String type array declaration in parent scope through lexical scoping rule. */
    var list: string[];

// An inner/child function declaration.
    function displayName() { // Inner/child function scope starts.

/* Following is an array (list) initialization in child/nested function. This array has been defined in a parent/outer scope. This is ok. */
        list = ["Type script", "Php", "Java script", ""];

// Loop through array’s each element and displayed.
        for (var index = 0; index < list.length; index++)
            document.write(list[index] + "<br/>");

/* Following is another string type variable initialization in child/nested function. */
        var siteName: string = "Tutorials Town";

    } // Inner/child function scope ends.
/* Following print statement is trying to access a variable in its parent function. This variable is declared inside child/nested scope body. Uncomment and run this line will cause a compile-time error because this variable (siteName) is not available in this scope, hence, this is un-accessible here and this is not ok. */   

// document.write("Website name: " + siteName);

// Call an inner/child function.


} // Outer/parent function scope ends.

/* Call an outer/parent function which calls an inner/child function. */

← 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 .