文本化

  • 版本网址缩略名: Core_JavaScript_1.5_Guide/文本化
  • 版本标题: 文本化
  • 版本 id: 276544
  • 创建于:
  • 创建者: Cnmahj
  • 是否是当前版本?
  • 评论 /* Floating-Point Literals */

修订内容

常数(Literals)

在JavaScript中,你可以使用常数来代替值。他们是固定的值,不是变量,让你从“字面上”理解脚本。这一节介绍了如下常数类型:

  • {{template.Anch("Array Literals")}}
  • {{template.Anch("Boolean Literals")}}
  • {{template.Anch("Floating-Point Literals")}}
  • {{template.Anch("Integers")}}
  • {{template.Anch("Object Literals")}}
  • {{template.Anch("String Literals")}}

数组常数(Literals)

数组常数就是被包围在中括号([])中的,零个以上表达式,每一个表达式是一个数组元素。当你用数组常数建立数组的时候,他会用你给的值来初始化他的元素,他的长度是你设定的元素数。

下面的例子,建立了一个叫coffees的数组,有三个元素,数组的长度为三。

var coffees = ["French Roast", "Colombian", "Kona"];

注意 数组常数是对象初始化器的一种。参见使用对象初始化器

如果在全局脚本中用常数定义了一个数组,则JavaScript每次使用时都要计算数组常数中的表达式的值。也就是说,在函数中使用的常数,每次调用函数是都会被创建。

数组常数也是数组对象。详细请参见数组对象

数组常数中多余的逗号

你不必列出数组常数中的所有元素。如果你在一行中写了两个逗号,数组也为没写上元素预留了空间。下面的例子建立了一个fish数组:

var fish = ["Lion", , "Angel"];

这个数组有两个有值的元素和一个空的元素(fish{{mediawiki.external(0)}}是"Lion"、fish{{mediawiki.external(1)}}undefinedfish{{mediawiki.external(2)}}是"Angel")。

如果你在元素列表的末尾附加了逗号,他们将被忽略。接下来的例子中,数组的长度是3。不存在元素myList{{mediawiki.external(3)}}。列表中的所有其他的逗号都会对应生成新元素。

var myList = ['home', , 'school', ];

下面的例子中,数组的长度是4,myList{{mediawiki.external(0)}}myList{{mediawiki.external(2)}}的值是未定义。

var myList = [ , 'home', , 'school'];

在下面的例子中,数组的长度是4,myList{{mediawiki.external(1)}}myList{{mediawiki.external(3)}}的值是未定义。只是最后面的多余逗号被忽略。

var myList = ['home', , 'school', , ];

布尔常数

布尔类型有两个常数值:truefalse

不要把原始的布尔值truefalse与布尔对象的真假值相混淆。布尔对象是围绕原始布尔数据类型的包装。详细请参见布尔对象

整数

证书可以表示为十进制、十六进制和八进制。十进制的整数用不带前导零的数字组成;在其前面加上前导零则为八进制;使用前导字符0x开始的是十六进制整数。十六进制整数的一位可以包括数字(0-9)和字母a-f或A-F。八进制使用数字0-7表示。

八进制整形常数在ECMA-262第三版中已经不推荐使用,并被删除了。为了向前兼容,JavaScript 1.5仍然支持。

一些整形常数的实例:

0、117、-345 (十进制)
015、0001、-077 (八进制)
0x1123、0x00111、-0xF1A7 (十六进制)

浮点常数

浮点常数可以由以下部分组成:

  • 带符号的十进制整数(以“+”或“-”开头);
  • 小数点(“.”);
  • 小数部分(另一个十进制数);
  • 指数。

指数部分是由“e”或“E”后跟带符号整数(以“+”或“-”开头)构成。浮点常数必须包括至少一位数字和一个小数点或“e”(或“E”)。

一些浮点数的例子 3.1415、-3.1E12、.1e12、2E-12。

简单的语法如下:

[数字][.数字][(E|e)[(+|-)]数字]

例如:

3.14
2345.789
.3333333333333333333

Object Literals

An object literal is a list of zero or more pairs of property names and associated values of an object, enclosed in curly braces ({}). You should not use an object literal at the beginning of a statement. This will lead to an error or not behave as you expect, because the { will be interpreted as the beginning of a block.

The following is an example of an object literal. The first element of the car object defines a property, myCar; the second element, the getCar property, invokes a function (CarTypes("Honda")); the third element, the special property, uses an existing variable (Sales).

var Sales = "Toyota";

function CarTypes(name) {
  if (name == "Honda")
     return name;
  else
     return "Sorry, we don't sell " + name + ".";
}

var car = { myCar: "Saturn", getCar: CarTypes("Honda"), special: Sales };

document.write(car.myCar); // Saturn
document.write(car.getCar); // Honda
document.write(car.special); // Toyota

Additionally, you can use a numeric or string literal for the name of a property or nest an object inside another. The following example uses these options.

var car = { manyCars: {a: "Saab", b: "Jeep"}, 7: "Mazda" };

document.write(car.manyCars.b); // Jeep
document.write(car[7]); // Mazda

Please note:

var foo = {a: "alpha", 2: "two"};
document.write(foo.a);    // alpha
document.write(foo[2]);   // two
//document.write(foo.2);  // Error: missing ) after argument list
//document.write(foo[a]); // Error: a is not defined
document.write(foo["a"]); // alpha
document.write(foo["2"]); // two

String Literals

A string literal is zero or more characters enclosed in double (") or single (') quotation marks. A string must be delimited by quotation marks of the same type; that is, either both single quotation marks or both double quotation marks. The following are examples of string literals:

  • "blah"
  • 'blah'
  • "1234"
  • "one line \n another line"
  • "John's cat"

You can call any of the methods of the String object on a string literal value—JavaScript automatically converts the string literal to a temporary String object, calls the method, then discards the temporary String object. You can also use the String.length property with a string literal:

  • "John's cat".length

You should use string literals unless you specifically need to use a String object. See String Object for details on String objects.

Using Special Characters in Strings

In addition to ordinary characters, you can also include special characters in strings, as shown in the following example.

"one line \n another line"

The following table lists the special characters that you can use in JavaScript strings.

Character Meaning
\b Backspace
\f Form feed
\n New line
\r Carriage return
\t Tab
\v Vertical tab
\' Apostrophe or single quote
\" Double quote
\\ Backslash character (\).
\XXX The character with the Latin-1 encoding specified by up to three octal digits XXX between 0 and 377. For example, \251 is the octal sequence for the copyright symbol.
\xXX The character with the Latin-1 encoding specified by the two hexadecimal digits XX between 00 and FF. For example, \xA9 is the hexadecimal sequence for the copyright symbol.
\uXXXX The Unicode character specified by the four hexadecimal digits XXXX. For example, \u00A9 is the Unicode sequence for the copyright symbol. See Unicode Escape Sequences.

Table 2.1: JavaScript special characters

Escaping Characters

For characters not listed in Table 2.1, a preceding backslash is ignored, but this usage is deprecated and should be avoided.

You can insert a quotation mark inside a string by preceding it with a backslash. This is known as escaping the quotation mark. For example:

var quote = "He read \"The Cremation of Sam McGee\" by R.W. Service.";
document.write(quote);

The result of this would be:

He read "The Cremation of Sam McGee" by R.W. Service.

To include a literal backslash inside a string, you must escape the backslash character. For example, to assign the file path c:\temp to a string, use the following:

var home = "c:\\temp";

{{template.PreviousNext("Core_JavaScript_1.5_Guide:Constants", "Core_JavaScript_1.5_Guide:Unicode")}}

{{ wiki.languages( { "en": "en/Core_JavaScript_1.5_Guide/Literals", "es": "es/Gu\u00eda_JavaScript_1.5/Literales", "fr": "fr/Guide_JavaScript_1.5/Constantes_litt\u00e9rales", "ja": "ja/Core_JavaScript_1.5_Guide/Literals", "ko": "ko/Core_JavaScript_1.5_Guide/Literals", "pl": "pl/Przewodnik_po_j\u0119zyku_JavaScript_1.5/Litera\u0142y" } ) }}

修订版来源

<p>
</p>
<h3 name=".E5.B8.B8.E6.95.B0.EF.BC.88Literals.EF.BC.89"> 常数(Literals) </h3>
<p>在JavaScript中,你可以使用常数来代替值。他们是固定的值,不是变量,让你从“字面上”理解脚本。这一节介绍了如下常数类型:
</p>
<ul><li> {{template.Anch("Array Literals")}}
</li><li> {{template.Anch("Boolean Literals")}}
</li><li> {{template.Anch("Floating-Point Literals")}}
</li><li> {{template.Anch("Integers")}}
</li><li> {{template.Anch("Object Literals")}}
</li><li> {{template.Anch("String Literals")}}
</li></ul>
<h4 name=".E6.95.B0.E7.BB.84.E5.B8.B8.E6.95.B0.EF.BC.88Literals.EF.BC.89"> 数组常数(Literals) </h4>
<p>数组常数就是被包围在中括号([])中的,零个以上表达式,每一个表达式是一个数组元素。当你用数组常数建立数组的时候,他会用你给的值来初始化他的元素,他的长度是你设定的元素数。
</p><p>下面的例子,建立了一个叫<code>coffees</code>的数组,有三个元素,数组的长度为三。
</p>
<pre class="eval">var coffees = ["French Roast", "Colombian", "Kona"];
</pre>
<p><b>注意</b> 数组常数是对象初始化器的一种。参见<a href="cn/Core_JavaScript_1.5_Guide/Creating_New_Objects/Using_Object_Initializers">使用对象初始化器</a>。
</p><p>如果在全局脚本中用常数定义了一个数组,则JavaScript每次使用时都要计算数组常数中的表达式的值。也就是说,在函数中使用的常数,每次调用函数是都会被创建。
</p><p>数组常数也是数组对象。详细请参见<a href="cn/Core_JavaScript_1.5_Guide/Predefined_Core_Objects/Array_Object">数组对象</a>。
</p><p><b>数组常数中多余的逗号</b>
</p><p>你不必列出数组常数中的所有元素。如果你在一行中写了两个逗号,数组也为没写上元素预留了空间。下面的例子建立了一个<code>fish</code>数组:
</p>
<pre class="eval">var fish = ["Lion", , "Angel"];
</pre>
<p>这个数组有两个有值的元素和一个空的元素(<code>fish{{mediawiki.external(0)}}</code>是"Lion"、<code>fish{{mediawiki.external(1)}}</code>是<code>undefined</code>、<code>fish{{mediawiki.external(2)}}</code>是"Angel")。
</p><p>如果你在元素列表的末尾附加了逗号,他们将被忽略。接下来的例子中,数组的长度是3。不存在元素<code>myList{{mediawiki.external(3)}}</code>。列表中的所有其他的逗号都会对应生成新元素。
</p>
<pre class="eval">var myList = ['home', , 'school', ];
</pre>
<p>下面的例子中,数组的长度是4,<code>myList{{mediawiki.external(0)}}</code>和<code>myList{{mediawiki.external(2)}}</code>的值是未定义。
</p>
<pre class="eval">var myList = [ , 'home', , 'school'];
</pre>
<p>在下面的例子中,数组的长度是4,<code>myList{{mediawiki.external(1)}}</code>和<code>myList{{mediawiki.external(3)}}</code>的值是未定义。只是最后面的多余逗号被忽略。
</p>
<pre class="eval">var myList = ['home', , 'school', , ];
</pre>
<h4 name=".E5.B8.83.E5.B0.94.E5.B8.B8.E6.95.B0"> 布尔常数 </h4>
<p>布尔类型有两个常数值:<code>true</code>和<code>false</code>。
</p><p>不要把原始的布尔值<code>true</code>、<code>false</code>与布尔对象的真假值相混淆。布尔对象是围绕原始布尔数据类型的包装。详细请参见<a href="cn/Core_JavaScript_1.5_Guide/Predefined_Core_Objects/Boolean_Object">布尔对象</a>。
</p>
<h4 name=".E6.95.B4.E6.95.B0"> 整数 </h4>
<p>证书可以表示为十进制、十六进制和八进制。十进制的整数用不带前导零的数字组成;在其前面加上前导零则为八进制;使用前导字符0x开始的是十六进制整数。十六进制整数的一位可以包括数字(0-9)和字母a-f或A-F。八进制使用数字0-7表示。
</p><p>八进制整形常数在ECMA-262第三版中已经不推荐使用,并被删除了。为了向前兼容,JavaScript 1.5仍然支持。
</p><p>一些整形常数的实例:
</p>
<pre class="eval">0、117、-345 (十进制)
015、0001、-077 (八进制)
0x1123、0x00111、-0xF1A7 (十六进制)
</pre>
<h4 name=".E6.B5.AE.E7.82.B9.E5.B8.B8.E6.95.B0"> 浮点常数 </h4>
<p>浮点常数可以由以下部分组成:
</p>
<ul><li> 带符号的十进制整数(以“+”或“-”开头);
</li><li> 小数点(“.”);
</li><li> 小数部分(另一个十进制数);
</li><li> 指数。
</li></ul>
<p>指数部分是由“e”或“E”后跟带符号整数(以“+”或“-”开头)构成。浮点常数必须包括至少一位数字和一个小数点或“e”(或“E”)。
</p><p>一些浮点数的例子 3.1415、-3.1E12、.1e12、2E-12。
</p><p>简单的语法如下:
</p>
<pre class="eval">[数字][.数字][(E|e)[(+|-)]数字]
</pre>
<p>例如:
</p>
<pre class="eval">3.14
2345.789
.3333333333333333333
</pre>
<h4 name="Object_Literals"> Object Literals </h4>
<p>An object literal is a list of zero or more pairs of property names and associated values of an object, enclosed in curly braces ({}). You should not use an object literal at the beginning of a statement. This will lead to an error or not behave as you expect, because the { will be interpreted as the beginning of a block.
</p><p>The following is an example of an object literal. The first element of the <code>car</code> object defines a property, <code>myCar</code>; the second element, the <code>getCar</code> property, invokes a function <code>(CarTypes("Honda"));</code> the third element, the <code>special</code> property, uses an existing variable (<code>Sales</code>).
</p>
<pre class="eval">var Sales = "Toyota";

function CarTypes(name) {
  if (name == "Honda")
     return name;
  else
     return "Sorry, we don't sell " + name + ".";
}

var car = { myCar: "Saturn", getCar: CarTypes("Honda"), special: Sales };

document.write(car.myCar); // Saturn
document.write(car.getCar); // Honda
document.write(car.special); // Toyota
</pre>
<p>Additionally, you can use a numeric or string literal for the name of a property or nest an object inside another. The following example uses these options.
</p>
<pre class="eval">var car = { manyCars: {a: "Saab", b: "Jeep"}, 7: "Mazda" };

document.write(car.manyCars.b); // Jeep
document.write(car[7]); // Mazda
</pre>
<p>Please note:
</p>
<pre class="eval">var foo = {a: "alpha", 2: "two"};
document.write(foo.a);    // alpha
document.write(foo[2]);   // two
//document.write(foo.2);  // Error: missing ) after argument list
//document.write(foo[a]); // Error: a is not defined
document.write(foo["a"]); // alpha
document.write(foo["2"]); // two
</pre>
<h4 name="String_Literals"> String Literals </h4>
<p>A string literal is zero or more characters enclosed in double (") or single (') quotation marks. A string must be delimited by quotation marks of the same type; that is, either both single quotation marks or both double quotation marks. The following are examples of string literals:
</p>
<ul><li> "blah"
</li><li> 'blah'
</li><li> "1234"
</li><li> "one line \n another line"
</li><li> "John's cat"
</li></ul>
<p>You can call any of the methods of the String object on a string literal value—JavaScript automatically converts the string literal to a temporary String object, calls the method, then discards the temporary String object. You can also use the <code>String.length</code> property with a string literal:
</p>
<ul><li> "John's cat".length
</li></ul>
<p>You should use string literals unless you specifically need to use a String object. See <a href="cn/Core_JavaScript_1.5_Guide/Predefined_Core_Objects/String_Object">String Object</a> for details on String objects.
</p>
<h5 name="Using_Special_Characters_in_Strings"> Using Special Characters in Strings </h5>
<p>In addition to ordinary characters, you can also include special characters in strings, as shown in the following example.
</p>
<pre class="eval">"one line \n another line"
</pre>
<p>The following table lists the special characters that you can use in JavaScript strings.
</p>
<table class="fullwidth-table">
<tbody><tr>
<th>Character</th>
<th>Meaning</th>
</tr>
<tr>
<td>\b</td>
<td>Backspace</td>
</tr>
<tr>
<td>\f</td>
<td>Form feed</td>
</tr>
<tr>
<td>\n</td>
<td>New line</td>
</tr>
<tr>
<td>\r</td>
<td>Carriage return</td>
</tr>
<tr>
<td>\t</td>
<td>Tab</td>
</tr>
<tr>
<td>\v</td>
<td>Vertical tab</td>
</tr>
<tr>
<td>\'</td>
<td>Apostrophe or single quote</td>
</tr>
<tr>
<td>\"</td>
<td>Double quote</td>
</tr>
<tr>
<td>\\</td>
<td>Backslash character (\).</td>
</tr>
<tr>
<td>\<i>XXX</i></td>
<td>The character with the Latin-1 encoding specified by up to three octal digits <i>XXX</i> between 0 and 377. For example, \251 is the octal sequence for the copyright symbol.</td>
</tr>
<tr>
<td>\x<i>XX</i></td>
<td>The character with the Latin-1 encoding specified by the two hexadecimal digits <i>XX</i> between 00 and FF. For example, \xA9 is the hexadecimal sequence for the copyright symbol.</td>
</tr>
<tr>
<td>\u<i>XXXX</i></td>
<td>The Unicode character specified by the four hexadecimal digits <i>XXXX</i>. For example, \u00A9 is the Unicode sequence for the copyright symbol. See <a href="cn/Core_JavaScript_1.5_Guide/Unicode#Unicode_Escape_Sequences">Unicode Escape Sequences</a>. </td>
</tr>
</tbody></table>
<p><small><b>Table 2.1: JavaScript special characters</b></small>
</p>
<h5 name="Escaping_Characters"> Escaping Characters </h5>
<p>For characters not listed in Table 2.1, a preceding backslash is ignored, but this usage is deprecated and should be avoided.
</p><p>You can insert a quotation mark inside a string by preceding it with a backslash. This is known as <i>escaping</i> the quotation mark. For example:
</p>
<pre class="eval">var quote = "He read \"The Cremation of Sam McGee\" by R.W. Service.";
document.write(quote);
</pre>
<p>The result of this would be:
</p>
<pre class="eval">He read "The Cremation of Sam McGee" by R.W. Service.
</pre>
<p>To include a literal backslash inside a string, you must escape the backslash character. For example, to assign the file path <code>c:\temp</code> to a string, use the following:
</p>
<pre class="eval">var home = "c:\\temp";
</pre>
<p>{{template.PreviousNext("Core_JavaScript_1.5_Guide:Constants", "Core_JavaScript_1.5_Guide:Unicode")}}
</p>
<div class="noinclude">
</div>
{{ wiki.languages( { "en": "en/Core_JavaScript_1.5_Guide/Literals", "es": "es/Gu\u00eda_JavaScript_1.5/Literales", "fr": "fr/Guide_JavaScript_1.5/Constantes_litt\u00e9rales", "ja": "ja/Core_JavaScript_1.5_Guide/Literals", "ko": "ko/Core_JavaScript_1.5_Guide/Literals", "pl": "pl/Przewodnik_po_j\u0119zyku_JavaScript_1.5/Litera\u0142y" } ) }}
恢复到这个版本