Udostępnij za pośrednictwem


Scope of Variables and Constants

JScript has three scopes: global, local, and class. If you declare a variable or constant outside a function or class definition, it is a global variable, and its value is accessible and modifiable throughout your program. If you declare a variable inside a function definition, that variable is local. It is created and destroyed every time the function is executed; it cannot be accessed from outside the function. If you declare a variable inside a class definition, that variable is available inside the class, and it cannot be accessed directly from the global scope. For more information, see Class-based Objects.

Discussion

Languages such as C++ also have "block scope"; any set of braces ({}) defines a new scope. JScript does not support block scopes.

A local variable can have the same name as a global variable, but it is entirely distinct and separate. Consequently, changing the value of one variable has no effect on the other. Inside the function in which the local variable is declared, only the local version has meaning. This is known as visibility.

// Define two global variables.
var name : String = "Frank";
var age : int = "34";

function georgeNameAge() {
   var name : String; // Define a local variable.
   name = "George";   // Modify the local variable.
   age = 42;          // Modify the global variable.
   print(name + " is " + age + " years old.");
}

print(name + " is " + age + " years old.");
georgeNameAge();
print(name + " is " + age + " years old.");

The output of this program shows that a local variable can be modified without changing the value of the global variable. Changes to the global variable from inside the function do affect the value in the global scope.

Frank is 34 years old.
George is 42 years old.
Frank is 42 years old.

Since JScript processes variable and constant declarations before executing any code, it does not matter whether the declaration is inside a conditional block or some other construct. Once JScript has found all the variables and constants, it executes the code in the function. This means that the value of a local constant is undefined until the constant declaration statement is reached and that a local variable is undefined until the variable is assigned to in the function.

Sometimes this results in unexpected behaviors. Consider the following program.

var aNumber = 100;
var anotherNumber = 200;
function tweak() {
   var s = "aNumber is " + aNumber + " and ";
   s += "anotherNumber is " + anotherNumber + "\n";
   return s;
   if (false)  {
      var aNumber;                // This statement is never executed.
      aNumber = 123;              // This statement is never executed.
      const anotherNumber = 42;   // This statement is never executed.
   } // End of the conditional.
} // End of the function definition.

print(tweak());

This output of this program is:

aNumber is undefined and anotherNumber is undefined

You might expect that aNumber would be 100 or 123 and that anotherNumber would be 200 or 42, but both values are undefined. Since both aNumber and anotherNumber are defined with local scope, they shadow the global variable and constant with the same name. Since the code that initializes the local variable and constant is never run, their values are undefined.

Explicit variable declaration is required in fast mode. When fast mode is turned off, implicit variable declaration is required. An implicitly declared variable inside a function—that is, one that appears on the left side of an assignment expression without the var keyword—is a global variable.

See Also

Concepts

Undefined Values

Other Resources

JScript Variables and Constants