Variablesˈvɛəriəbl变量

A variable is a name associated with a value; we say that the variable stores or contains the value.
变量是一个和数值相关的名字,我们说变量“存储”了或“包含”了那个值。

Variables allow you to store and manipulate data in your programs.
有了变量,就可以在程序中存储和操作数据了。

For example, the following line of JavaScript assigns the value 2 to a variable named i:
例如,下面一行JavaScript代码将数值 2 赋给了一个名为 i 的变量
i = 2;

And the following line adds 3 to i and assigns the result to a new variable, sum:
下面的代码将3加到i上,然后把结赋给了一个新的变量 sum:
var sum = i + 3;

Variable Typling 变量的类型

JavaScript is untyped. This means, in part, that a JavaScript variable can hold a value of any datatype.
JavaScript 是非类型的,这就意味着 JavaScript 的变量可以存放任何类型的值。

For example, it is perfectly legal in JavaScript to assign a number to a variable and then later assign a string to that variable:
例如,在JavaScript中,可以先把一个数值赋给一个变量,然后再把一个字符串赋给它,这是完全合法的:
i = 10;<br /> i = "ten";

Variable Declaration 变量的声明

Before you use a variable in a JavaScript program, you must declare it.
JavaScript程序中,在使用一个变量之前,必须先声明它。

If you don’t declare a variable explicitly, JavaScript will declare it implicitly for you.
如果不显式地声明一个变量,JavaScript将隐式地声明它。

Variables are declared with the var keyword, like this:
变量是使用关键字var声明的,如下所示:
var i;<br /> var sum;

You can also declare multiple variables with the same var keyword:
也可以使用一个var关键字声明多个变量:
var i, sum;

And you can combine variable declaration with variable initialization:
而且还可以将变量声明和变量初始化绑定在一起:
var message = "hello";<br /> var i = 0, j = 0, k = 0;

If you don’t specify an initial value for a variable with the var statement, the variable is declared, but its initial value is undefined until your code stores a value into it.
如果没有用var语句给一个变量指定初始值,那么虽然这个变量被声明了,但是在给它存入一个值之前,它的初始值就是 undefined

Note that the var statement can also appear as part of the for and for/in loops, allowing you to succinctly declare the loop variable as part of the loop syntax itself. For example:
注意,Var语句还可以作为for循环和for/in循环的一部分,这样就使循环变量的声明成为了循环语法自身的一部分,非常简洁,例如:

for(var i = 0; i < 10; i++) document.write(i, “
“);
for(var i = 0; j = 10; i < 10; i++, j–) document.write(i*j, “
“);
for(var i in o) document.write(i, “
“);

Variables declared with var are permanent: attempting to delete them with the delete operator causes an error.
由var声明的变量是永久性的,也就是说,用delete运算符来删除这些变量将会引发错误。

Repeated and Omitted Declarations 重复的声明和遗漏的声明

It is legal and harmless to declare a variable more than once with the var statement.
使用var语句多次声明同一个变量,不仅是合法的,而且也不会造成任何错误。

If the repeated declaration has an initializer, it acts as if it were simply an assignment statement.
如果重复的声明有一个初始值,那么它担当的不过是一个赋值语句的角色。

If you attempt to read the value of an undeclared variable, JavaScript generates an error.
如果尝试读一个未声明的变量的值,JavaScript会生成一个错误。

If you assign a value to a variable that you have not declared with var, JavaScript implicitly declares that variable for you.
如果尝试给一个未用var声明的变量赋值,JavaScript会隐式声明该变量。

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 to use within a single function, you must always use the var statement within function bodies.
局部变量只在一个函数中使用,要防止在创建局部变量时创建全局变量(或采用已有的全局变量),就必须在函数体内部使用var语句。

It’s best to use var for all variables, whether global or local.
无论是全局变量还是局部变量,最好都使用var语句创建。

Variable Scope 变量的作用域

The scope of a variable is the region of your program in which it is defined.
一个变量的作用域是程序中定义这个变量的区域。

A global variable has global scope; it is defined everywhere in your JavaScript code.
全局变量的作用域是全局性的,即在JavaScript代码中,它处处都有定于。

On the other hand, variables declared within a function are defined only within the body of the function.
而在函数之内声明的变量,就只在函数体内部有定义。

They are local variables and have local scope.
它们是局部变量,作用域是局限性的。

Function parameters also count as local variables and are defined only within the body of the function.
函数的参数也是局部变量,它们只在函数体内部有定义。

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”:
例如,下面的代码将输出单词“local”:

var scope = “global”; // 声明一个全局变量
function checkscope( ) {
var scope = “local”; // 声明一个与全局变量同名的局部变量
document.write(scope); // 使用本地变量
}
checkscope( ); // 输出单词 “local”

Referenceˈrefrəns参考

《JavaScript权威指南》