作者shyangs (欲翔)
看板Ajax
标题Re: [ js ] 全域和区域变数
时间Tue Jun 17 01:55:32 2008
函式内的叫区域变数
函式外的叫全域变数
参考资料:w3school、Mozilla 开发者中心 (这两个够权威了吧- -)
http://www.w3schools.com/js/js_functions.asp
The Lifetime of JavaScript 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.
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.
http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Guide:Variables
When you declare a variable outside of any function, it is called a global
variable, because it is available to any other code in the current document.
When you declare a variable within a function, it is called a local variable,
because it is available only within that function.
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 140.122.231.24
※ 编辑: shyangs 来自: 140.122.231.24 (06/17 01:56)
1F:→ zhadow:如果是用var 宣告的,上面的说法没问题. 06/17 05:56
2F:→ zhadow:如果不是用 var 宣告的,就不对. 06/17 05:57
4F:→ zhadow:By simply assigning it a value. For example, x = 42 06/17 06:00
5F:→ zhadow:This always declares a global variable and 06/17 06:01
6F:→ zhadow:generates a strict JavaScript warning. 06/17 06:01
7F:→ zhadow:You shouldn't use this variant. 06/17 06:02