JavaScript: The Definitive Guide, Sixth Editio javaScript权威指南(第6版) pdf 文字版-文字版, javascript电子书, 和javascript 有关的电子书:

5.3.1 var

5.3.1 var


The var statement declares a variable or variables. Here’s the syntax: var name_1 [ = value_1] [ ,..., name_n [= value_n]]

The var keyword is followed by a comma-separated list of variables to declare; each variable in the list may optionally have an initializer expression that specifies its initial value. For example:

var i; // One simple variable

var j = 0; // One var, one value

var p, q; // Two variables

var greeting = "hello" + name; // A complex initializer

var x = 2.34, y = Math.cos(0.75), r, theta; // Many variables

var x = 2, y = x*x; // Second var uses the first

var x = 2, // Multiple variables...

f = function(x) { return x*x }, // each on its own line

y = f(x);

If a var statement appears within the body of a function, it defines local variables, scoped to that function. When varis used in top-level code, it declares global variables, visible throughout the JavaScript program. As noted in §3.10.2 , global variables are properties of the global object. Unlike other global properties, however, properties created with var cannot be deleted.

If no initializer is specified for a variable with the var statement, the variable’s initial value is undefined. As described in §3.10.1 , variables are defined throughout the script or function in which they are declared—their declaration is “hoisted” up to the start of the script or function. Initialization, however, occurs at the location of the var statement, and the value of the variable is undefined before that point in the code.

Note that the var statement can also appear as part of the for and for/in loops. (These variables are hoisted, just like variables declared outside of a loop.) Here are examples repeated from §3.9 :

for(var i = 0; i < 10; i++) console.log(i); for(var i = 0, j=10; i < 10; i++,j--) console.log(i*j); for(var i in o) console.log(i);

Note that it is harmless to declare the same variable multiple times.

欢迎转载,转载请注明来自一手册:http://yishouce.com/book/1/31333.html
友情链接It题库(ittiku.com)| 版权归yishouce.com所有| 友链等可联系 admin#yishouce.com|粤ICP备16001685号-1