变量

  • 版本网址缩略名: Core_JavaScript_1.5_Guide/变量
  • 版本标题: 变量
  • 版本 id: 276559
  • 创建于:
  • 创建者: Cnmahj
  • 是否是当前版本?
  • 评论 /* Evaluating Variables */

修订内容

变量

在应用程序中使用变量作为值的符号名。变量名又叫做“标识符”,要符合一定规则。

一个JavaScript标识符必须以字母、下划线(_)或者美元符号($)作为首字符。随后的字符也可以是数字。因为JavaScript大小写敏感,所以字母包含“A”到“Z”(大写)和“a”到“z”(小写)。

从JavaScript 1.5开始,你可以在标识符中使用ISO 8859-1或者Unicode字母,比如“å”和“ü”。你也可以在标识符中使用转义序列页面的\uXXXX Unicode转义序列。

合格的标识符如Number_hitstemp99_name

定义变量

你可以使用两种方式来定义变量:

  • 使用关键字 var。例如, var x = 42。 这个语法可以用来声明局部和全局变量。
  • 对变量进行简单赋值。 例如, x = 42。 这常常用来声明全局变量,并生成一个严格的JavaScript警告。 你不应该使用形式。

变量求值

一个用var声明的,没有初始值的变量的值是未定义

访问一个未声明的变量,将导致参照错误的异常被抛出(ReferenceError exception)。例如:

var a;
print("The value of a is " + a); // 输出“The value of a is undefined”
print("The value of b is " + b); // 抛出ReferenceError异常

你可以使用undefined去检测一个变量是否有值。下面的代码中,变量input没有被赋值,if语句求得的值是true

var input;
if(input === undefined){
  doThis();
} else {
  doThat();
}

Not sure how the following is related to "Variables" section 作为布尔内容时,undefined的值是false。例如,下面的代码会执行myFunction,因为myArray的元素未定义的。

var myArray = new Array();
if (!myArray[0]) myFunction();

当你对null变量求值时,对于数值内容它是0,布尔内容它是false。例如:

var n = null;
print(n * 32); // 输出0

Variable Scope

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.

JavaScript does not have block statement scope; rather, it will be local to the code that the block resides within. For example the following code will print 5 instead of throwing if condition is false:

if (condition) {
  var x = 5;
}
print(x ? x : 0);

Another unusual thing about variables in JavaScript is that you can refer to a variable declared later, without getting an exception:

print(x === undefined); // prints "true"
var x = 3;

Global Variables

need links to pages discussing scope chains and the global object Global variables are in fact properties of the global object. In web pages the global object is window, so you can set and access global variables using the window.variable syntax.

Consequently, you can access global variables declared in one window or frame from another window or frame by specifying the window or frame name. For example, if a variable called phoneNumber is declared in a FRAMESET document, you can refer to this variable from a child frame as parent.phoneNumber.

See Also

Sharp variables in JavaScript

{{template.PreviousNext("Core_JavaScript_1.5_Guide:Values", "Core_JavaScript_1.5_Guide:常量")}}

{{ wiki.languages( { "es": "es/Gu\u00eda_JavaScript_1.5/Variables", "fr": "fr/Guide_JavaScript_1.5/Variables", "ja": "ja/Core_JavaScript_1.5_Guide/Variables", "ko": "ko/Core_JavaScript_1.5_Guide/Variables", "pl": "pl/Przewodnik_po_j\u0119zyku_JavaScript_1.5/Zmienne", "zh-cn": "cn/Core_JavaScript_1.5_Guide/\u53d8\u91cf" } ) }}

修订版来源

<p>
</p>
<h3 name=".E5.8F.98.E9.87.8F"> 变量 </h3>
<p>在应用程序中使用变量作为值的符号名。变量名又叫做“标识符”,要符合一定规则。
</p><p>一个JavaScript标识符必须以字母、下划线(_)或者美元符号($)作为首字符。随后的字符也可以是数字。因为JavaScript大小写敏感,所以字母包含“A”到“Z”(大写)和“a”到“z”(小写)。
</p><p>从JavaScript 1.5开始,你可以在标识符中使用ISO 8859-1或者Unicode字母,比如“å”和“ü”。你也可以在标识符中使用<a href="cn/Core_JavaScript_1.5_Guide/Unicode#Unicode_Escape_Sequences">转义序列</a>页面的\uXXXX Unicode转义序列。
</p><p>合格的标识符如<code>Number_hits</code>、<code>temp99</code>和<code>_name</code>。
</p>
<h4 name=".E5.AE.9A.E4.B9.89.E5.8F.98.E9.87.8F"> 定义变量 </h4>
<p>你可以使用两种方式来定义变量:
</p>
<ul><li> 使用关键字 <a href="cn/Core_JavaScript_1.5_Reference/Statements/var">var</a>。例如, <code>var x = 42</code>。 这个语法可以用来声明<a href="#Variable_Scope">局部和全局</a>变量。
</li><li> 对变量进行简单赋值。 例如, <code>x = 42</code>。 这常常用来声明<a href="#Global_Variables">全局变量</a>,并生成一个严格的JavaScript警告。 你不应该使用形式。
</li></ul>
<h4 name=".E5.8F.98.E9.87.8F.E6.B1.82.E5.80.BC"> 变量求值 </h4>
<p>一个用var声明的,没有初始值的变量的值是<a href="cn/Core_JavaScript_1.5_Reference/Global_Properties/undefined">未定义</a>。
</p><p>访问一个未声明的变量,将导致参照错误的异常被抛出(ReferenceError exception)。例如:
</p>
<pre class="eval">var a;
print("The value of a is " + a); // 输出“The value of a is undefined”
print("The value of b is " + b); // 抛出ReferenceError异常
</pre>
<p>你可以使用<code>undefined</code>去检测一个变量是否有值。下面的代码中,变量<code>input</code>没有被赋值,<code><a href="cn/Core_JavaScript_1.5_Reference/Statements/if...else">if</a></code>语句求得的值是<code>true</code>。
</p>
<pre class="eval">var input;
if(input === undefined){
  doThis();
} else {
  doThat();
}
</pre>
<p><span class="comment">Not sure how the following is related to "Variables" section</span>
作为布尔内容时,<code>undefined</code>的值是<code>false</code>。例如,下面的代码会执行<code>myFunction</code>,因为<code>myArray</code>的元素未定义的。
</p>
<pre class="eval">var myArray = new Array();
if (!myArray[0]) myFunction();
</pre>
<p>当你对null变量求值时,对于数值内容它是0,布尔内容它是false。例如:
</p>
<pre class="eval">var n = null;
print(n * 32); // 输出0
</pre>
<h4 name="Variable_Scope"> Variable Scope </h4>
<p>When you declare a variable outside of any function, it is called a <i>global</i> 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 <i>local</i> variable, because it is available only within that function.
</p><p>JavaScript does not have <a href="cn/Core_JavaScript_1.5_Guide/Block_Statement#Block_Statement">block statement</a> scope; rather, it will be local to the code that the block resides within. For example the following code will print 5 instead of throwing if <code>condition</code> is <code>false</code>:
</p>
<pre class="eval">if (condition) {
  var x = 5;
}
print(x ? x : 0);
</pre>
<p>Another unusual thing about variables in JavaScript is that you can refer to a variable declared later, without getting an exception:
</p>
<pre class="eval">print(x === undefined); // prints "true"
var x = 3;
</pre>
<h4 name="Global_Variables"> Global Variables </h4>
<p><span class="comment">need links to pages discussing scope chains and the global object</span>
Global variables are in fact properties of the <i>global object</i>. In web pages the global object is <a href="cn/DOM/window">window</a>, so you can set and access global variables using the <code>window.<i>variable</i></code> syntax.
</p><p>Consequently, you can access global variables declared in one window or frame from another window or frame by specifying the window or frame name. For example, if a variable called <code>phoneNumber</code> is declared in a <code>FRAMESET</code> document, you can refer to this variable from a child frame as <code>parent.phoneNumber</code>.
</p>
<h4 name="See_Also"> See Also </h4>
<p><a href="cn/Sharp_variables_in_JavaScript">Sharp variables in JavaScript</a>
</p><p>{{template.PreviousNext("Core_JavaScript_1.5_Guide:Values", "Core_JavaScript_1.5_Guide:常量")}}
</p>
<div class="noinclude">
</div>
{{ wiki.languages( { "es": "es/Gu\u00eda_JavaScript_1.5/Variables", "fr": "fr/Guide_JavaScript_1.5/Variables", "ja": "ja/Core_JavaScript_1.5_Guide/Variables", "ko": "ko/Core_JavaScript_1.5_Guide/Variables", "pl": "pl/Przewodnik_po_j\u0119zyku_JavaScript_1.5/Zmienne", "zh-cn": "cn/Core_JavaScript_1.5_Guide/\u53d8\u91cf" } ) }}
恢复到这个版本