日期:2014-05-16 浏览次数:20384 次
?
If you attempt to read the value of an undeclared variable, JavaScript will generate an error. If you assign a value to a variable that you have not declared with var , JavaScript will implicitly declare that variable for you. Note, however, that implicitly declared variables are always created as global variables, even if they are used within the body of a function. To prevent the creation of a global variable (or the use of an existing global variable) when you meant to create a local variable for use within a single function, you must always use the var statement within function bodies. It's best to use var for all variables, whether global or local. (The distinction between local and global variables is explored in more detail in the next section.)
?
?
Within the body of a function, a local variable takes precedence over a global variable with the same name. If you declare a local variable or function parameter with the same name as a global variable, you effectively hide the global variable. For example, the following code prints the word "local":
?
var scope = "global"; // Declare a global variable function checkscope( ) { var scope = "local"; // Declare a local variable with the same name document.write(scope); // Use the local variable, not the global one } checkscope( ); // Prints "local"?
?
Although you can get away with not using the var statement when you write code in the global scope, you must always use var to declare local variables. Consider what happens if you don't:
?
scope = "global"; // Declare a global variable, even without var function checkscope( ) { scope = "local"; // Oops! We just changed the global variable document.write(scope); // Uses the global variable myscope = "local"; // This implicitly declares a new global variable document.write(myscope); // Uses the new global variable } checkscope( ); // Prints "locallocal" document.write(scope); // This prints "local" document.write(myscope); // This prints "local"?
In general, functions do not know what variables are defined in the global scope or what they are being used for. Thus, if a function uses a global variable instead of a local one, it runs the risk of changing a value upon which some other part of the program relies. Fortunately, avoiding this problem is simple: declare all variables with var .
In JavaScript 1.2 (and ECMAScript v3), function definitions can be nested. Each function has its own local scope, so it is possible to have several nested layers of local scope. For example:
?
var scope = "global scope"; // A global variable function checkscope( ) { var scope = "local scope"; // A local variable function nested( ) { var scope = "nested scope"; // A nested scope of local variables document.write(scope); // Prints "nested scope" } nested( ); } checkscope( );?
Note that unlike C, C++, and Java, JavaScript does not have block-level scope. All variables declared in a function, no matter where they are declared, are defined throughout the function. In the following code, the variables i , j , and k all have the same scope: all three are defined throughout the body of the function. This would not be the case if the code were written in C, C++, or Java:
?