JS 变量

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

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值