当前位置: 首页 > 网络学院 > 客户端脚本教程 > JavaScript > JS 变量

JavaScript
JS 介绍
JS 怎样使用
JS 在哪使用
JS 变量
JS If...Else
JS Switch
JS 操作符
JS Popup Boxes
JS 函数
JS For 循环
JS While 循环
JS Break 循环
JS For...In
JS 事件
JS Try...Catch
JS Throw
JS onerror
JS 特殊字符
JS Guidelines
JS 对象介绍

JavaScript 中的 JS 变量


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-01   浏览: 1248 ::
收藏到网摘: n/a

A variable is a "container" for information you want to store.
变量是你想要存储信息的“容器”


Examples
例子

Variable[变量]
Variables are used to store data. This example will show you how.
这个例子将给你展示怎样来存储数据


Variables
变量

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:
变量名称规则:

  • Variable names are case sensitive
    是区分大小写的
  • They must begin with a letter or the underscore character
    开始部分必须为一个字母或是下划线
  • 变量名不能有空格或者除下划线之外的其他标点符号,可以是数字,字母和下划线
  • 长度没有限制,但是必须放在同一行中

IMPORTANT! JavaScript is case-sensitive! A variable named strname is not the same as a variable named STRNAME!
重点注意!JS是区分大小写的!一个名称为strname的变量和名为STRNAME的变量是不同的


Declare a Variable
声明变量

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

 


Assign a Value to a Variable
给变量指定值

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"。


Lifetime of Variables
变量的寿命(有效时间和范围)

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.
如果你在函数外定义一个变量,那页面里所有的函数都可以访问它。它的有效范围从指定开始直到你关闭页面才会结束。

评论 (2) 1 All

登陆 | 还没注册?