Scope is the area or location within the program where a variable or function is accessible.

Lexical also called static Scoping defines how variable names are resolved in nested functions: inner functions contain the scope of parent functions even if the parent function has returned.

 
 var scope = "I am global scope";
 function whatismyscope(){
    var scope = "I am local scope";
    
    function getScope() { return scope; }
    
    return getScope;
 }
 whatismyscope()() 
 

The above code will return “I am local scope“. It will not return “I am a global“. Because the function getScope () counts where is was originally defined which is under the scope of function whatismyscope.

It will not bother from whatever it is being called (the global scope/from within another function even), that’s why global scope value I am global will not be printed.

IBM defines it as:


The portion of a program or segment unit in which a declaration applies. An identifier declared in a routine is known within that routine and within all nested routines. If a nested routine declares an item with the same name, the outer item is not available in the nested routine.


For more detail: 

http://stackoverflow.com/questions/1047454/what-is-lexical-scope
http://pierrespring.com/2010/05/11/function-scope-and-lexical-scoping/

Leave Comment

Your email address will not be published. Required fields are marked *

en_USEnglish
en_USEnglish