当前位置: 首页 > 网络学院 > 客户端脚本教程 > JavaScript > JS 变量
A variable is a "container" for information you want to store.
变量是你想要存储信息的“容器”
Variable[变量]
Variables are used to store data. This example will show you how.
这个例子将给你展示怎样来存储数据
A variable is a "container" for information you want to store. A variable's value can change during the script. You can refer to a variable by name to see its value or to change its value.
变量是你想要存储数据的“容器”。变量的值可以在脚本中改变。你可以调用变量的名称来看看它的值或是改变它的值
Rules for variable names:
变量名称规则:
IMPORTANT! JavaScript is case-sensitive! A variable named strname is not the same as a variable named STRNAME!
重点注意!JS是区分大小写的!一个名称为strname的变量和名为STRNAME的变量是不同的
You can create a variable with the var statement:
你可以通过"var"语句建立一个变量:
var strname = some value |
You can also create a variable without the var statement:
你也可以不用var来建立变量:
strname = some value |
You can assign a value to a variable like this:
可以用这样的方法来给变量指定值:
var strname = "Hege" |
Or like this:
或者这样:
strname = "Hege" |
The variable name is on the left side of the expression and the value you want to assign to the variable is on the right. Now the variable "strname" has the value "Hege".
变量名称写在表达式的左边,你想要指定的值写在右边。现在变量名称为"strname"的变量值为"Hege"。
When you declare a variable within a function, the variable can only be accessed within that function. When you exit the function, the variable is destroyed. These variables are called local variables. You can have local variables with the same name in different functions, because each is recognized only by the function in which it is declared.
当你在function(函数)里指定一个变量,它就只能在该函数内进行访问。当你离开函数变量就无效了。这样的变量可以称作局部变量。你可以在不同的函数内使用同样名称的变量,因为在函数中只会辨认它所指定的变量(别的函数怎么定义是不管的)
If you declare a variable outside a function, all the functions on your page can access it. The lifetime of these variables starts when they are declared, and ends when the page is closed.
如果你在函数外定义一个变量,那页面里所有的函数都可以访问它。它的有效范围从指定开始直到你关闭页面才会结束。