Language - Variable (Scope|Execution Context) - lexical scope

> Code - (Programming|Computer) Language > Code - (Engine|Runtime)

1 - About

The scope of a variable is the context in which it's visible to the program.

Scope determines where and how identifiers (variable, function) are stored.

Many languages use block-level scope, in which variables exist only within this current “block” of code but some other use the function-level as javascript

See also: Language - Namespace (Scoping rule for name)

Scope is basically a collection of variables as well as the rules for how those variables are accessed by name.

A variable name has to be unique within the same scope. But the same variable name a could appear in different scopes.

A scope can be nested inside another scope. If one scope is nested inside another, code inside the innermost scope can access variables from either scope.

Lexical scope rules say that code in one scope can access variables of either that scope or any scope outside of it.

Advertising

3 - Model

  • Lexical Scope - The most common
  • Dynamic Scope (such as Bash scripting, some modes in Perl, etc.)

4 - Nested

As the scope is generally dependent of the parse tree, scope are nested.

5 - Example

  • global variables: are available everywhere
  • member variables: are only available to members of a certain class
  • instance variables: are only available to particular instances of a class