Baseline Widely available
宣åä¸åè®æ¸, åæå¯ä»¥éå¼·å¶æ§å°è³¦äºä¸åå§å¼ã
èªæ³var varname1 [= value1 [, varname2 [, varname3 ... [, varnameN]]]];
varnameN
è®æ¸å稱ãå¯ä»¥æ¯ä»»ä½åæ³çèå¥å符 (identifier)ã
valueN
è®æ¸çåå§å¼ãå¯ä»¥æ¯ä»»ä½åæ³çè¡¨ç¤ºå¼ (expression)ã
以 var
宣åçè®æ¸, å
¶ä½ç¨ç¯å (scope) åæ¼è©²å½æ¸ä¹å
§; 使¯å¦æå¨å½æ¸å¤å®£å, å
¶ä½ç¨ç¯ååçºå
¨åæ§ (global) (亦å³å
ç´æ¼å
¨åç©ä»¶ä¹å
§)ã
å¨å½æ¸ä¹å¤ä½¿ç¨ä»¥ var
宣åçè®æ¸æ¯éå¼·å¶ç (optional); 妿å°ä¸åæªç¶å®£åçè®æ¸è³¦å¼, å®æè¢«æä¸ (implicitly) 宣åæçºä¸åå
¨åè®æ¸ (äº¦å³æçºå
¨åç©ä»¶ç屬æ§)ãå
¶ä¸å·®ç°å¨æ¼, 已宣åçè®æ¸æ¯å
¨åç©ä»¶è£¡çä¸åç¡æ³è®æ´ (non-configurable) ç屬æ§, èæªå®£åçè®æ¸åæ¯å¯è®æ´ç (configurable)ã
å æ¤, 建è°ä½ ä¸å®è¦å®£åä½ çè®æ¸, ä¸ç®¡ä½ è¦å°å®ä½¿ç¨æ¼å ¨åç¯åå §æè 彿¸å §ã
è¥æªå®£åè®æ¸, å°é常å¯è½å°è´ç¡æ³é 測ççµæãæä»¥, å¨ ECMAScript 5 strict mode ä¸, è¥å¨å½æ¸ä¸çµ¦ä¸åæªç¶å®£åç彿¸è³¦å¼, å°æä¸åºé¯èª¤ã
Variable declarations, wherever they occur, are processed before any code is executed. The scope of a variable declared with var
is its current execution context, which is either the enclosing function or, for variables declared outside any function, global.
Assigning a value to an undeclared variable implicitly creates it as a global variable (it becomes a property of the global object) when the assignment is executed. The differences between declared and undeclared variables are:
1. Declared variables are constrained in the execution context in which they are declared. Undeclared variables are always global.
function x() {
y = 1; // Throws a ReferenceError in strict mode
var z = 2;
}
x();
console.log(y); // logs "1"
console.log(z); // Throws a ReferenceError: z is not defined outside x
2. Declared variables are created before any code is executed. Undeclared variables do not exist until the code assigning to them is executed.
console.log(a); // Throws a ReferenceError.
console.log("still going..."); // Never executes.
var a;
console.log(a); // logs "undefined" or "" depending on browser.
console.log("still going..."); // logs "still going...".
3. Declared variables are a non-configurable property of their execution context (function or global). Undeclared variables are configurable (e.g. can be deleted).
var a = 1;
b = 2;
delete this.a; // Throws a TypeError in strict mode. Fails silently otherwise.
delete this.b;
console.log(a, b); // Throws a ReferenceError.
// The 'b' property was deleted and no longer exists.
Because of these three differences, failure to declare variables will very likely lead to unexpected results. Thus it is recommended to always declare variables, regardless of whether they are in a function or global scope. And in ECMAScript 5 strict mode, assigning to an undeclared variable throws an error.
var hoistingå¨ JavaScript ä¸, è®æ¸å¯ä»¥å 使ç¨å宣åã
å æ¤, 建è°ä½ æ°¸é é½æè®æ¸ç宣忾å¨å½æ¸çæé 端ãå¦åå¯è½å°è´æ··äºçæ æ³ã
Because variable declarations (and declarations in general) are processed before any code is executed, declaring a variable anywhere in the code is equivalent to declaring it at the top. This also means that a variable can appear to be used before it's declared. This behavior is called "hoisting", as it appears that the variable declaration is moved to the top of the function or global code.
bla = 2;
var bla;
// ...
// is implicitly understood as:
var bla;
bla = 2;
For that reason, it is recommended to always declare variables at the top of their scope (the top of global code and the top of function code) so it's clear which variables are function scoped (local) and which are resolved on the scope chain.
ç¯ä¾ Declaring and initializing two variables Assigning two variables with single string valuevar a = "A";
var b = a;
// Equivalent to:
var a,
b = (a = "A");
Be mindful of the order:
var x = y,
y = "A";
console.log(x + y); // undefinedA
Here, x
and y
are declared before any code is executed, the assignments occur later. At the time "x = y
" is evaluated, y
exists so no ReferenceError
is thrown and its value is 'undefined
'. So, x
is assigned the undefined value. Then, y
is assigned a value of 'A'. Consequently, after the first line, x === undefined && y === 'A'
, hence the result.
var x = 0;
function f() {
var x = (y = 1); // x is declared locally. y is not!
}
f();
console.log(x, y); // Throws a ReferenceError in strict mode (y is not defined). 0, 1 otherwise.
// In non-strict mode:
// x is the global one as expected
// y leaked outside of the function, though!
Implicit globals and outer function scope
Variables that appear to be implicit globals may be references to variables in an outer function scope:
var x = 0; // x is declared global, then assigned a value of 0
console.log(typeof z); // undefined, since z doesn't exist yet
function a() {
// when a is called,
var y = 2; // y is declared local to function a, then assigned a value of 2
console.log(x, y); // 0 2
function b() {
// when b is called
x = 3; // assigns 3 to existing global x, doesn't create a new global var
y = 4; // assigns 4 to existing outer y, doesn't create a new global var
z = 5; // creates a new global variable z and assigns a value of 5.
} // (Throws a ReferenceError in strict mode.)
b(); // calling b creates z as a global variable
console.log(x, y, z); // 3 4 5
}
a(); // calling a also calls b
console.log(x, z); // 3 5
console.log(typeof y); // undefined as y is local to function a
è¦ç¯ ç覽å¨ç¸å®¹æ§ åè¦
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4