const

  • 版本网址缩略名: JavaScript/Reference/Statements/const
  • 版本标题: const
  • 版本 id: 239077
  • 创建于:
  • 创建者: ziyunfei
  • 是否是当前版本?
  • 评论 2 words added, 7 words removed

修订内容

概述

声明一个只读的,命名的常量.

版本信息

Statement
Implemented in: JavaScript 1.5, NES 6.0 (Netscape extension, C engine only)

语法

const varname1 = value1 [, varname2 = value2 [, varname3 = value3 [, ... [, varnameN = valueN]]]];

参数meters

varnameN
Constant name. It can be any legal identifier.
valueN
Value of the constant. It can be any legal expression.

描述

Creates a constant that can be global or local to the function in which it is declared. Constants follow the same scope rules as variables.

The value of a constant cannot change through re-assignment, and a constant cannot be re-declared. Because of this, although it is possible to declare a constant without initializing it, it would be useless to do so.

A constant cannot share its name with a function or a variable in the same scope.

浏览器兼容性

The current implementation of const is a Mozilla-specific extension and is not part of ECMAScript 5. It is supported in Firefox & Chrome (V8). As of Safari 5.1.7 and Opera 12.00, if you define a variable with const in these browsers, you can still change its value later. It is not supported in Internet Explorer 6-9, or in the preview of Internet Explorer 10. The const keyword currently declares the constant in the function scope (like variables declared with var).

FIrefox, as of version 13, will throw a TypeError if you try to redefine a constant. None of the major browsers will produce any notices or errors if you later try to assign another value to a constant. In all these browsers, the return value of such an operation will be that of the new value assigned. Only in Firefox and Chrome (as of version 20) will the reassignment be unsuccessful.

const is going to be defined by ECMAScript 6, but with different semantics. Similar to variables declared with the let statement, constants declared with const will be block-scoped.

例子

The following example produces the output "a is 7."

const a = 7;
console.log("a is " + a + ".");

相关链接

{{ languages( { "fr": "fr/R\u00e9f\u00e9rence_de_JavaScript_1.5_Core/Instructions/const", "ja": "ja/Core_JavaScript_1.5_Reference/Statements/const", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Polecenia/const" ,"en":"en/JavaScript/Reference/Statements/const"} ) }}

修订版来源

<h2>概述</h2>
<p>声明一个只读的,命名的常量.</p><h2>版本信息</h2>
<table class="standard-table"> <tbody> <tr> <td class="header" colspan="2">Statement</td> </tr> <tr> <td>Implemented in:</td> <td>JavaScript 1.5, NES 6.0 (Netscape extension, <a href="/zh-cn/SpiderMonkey" title="zh-cn/SpiderMonkey">C engine</a> only)</td> </tr> </tbody>
</table>
<h2>语法</h2>
<p><code>const <em>varname1 = <em>value1 [</em>, <em>varname2</em> = <em>value2 [</em>, <em>varname3</em> = <em>value3 [</em>, ... [</em>, <em>varnameN</em> = <em>valueN]]]]</em>;</code></p>
<h2>参数meters</h2>
<dl> <dt><code>varnameN</code></dt> <dd>Constant name. It can be any legal identifier.</dd>
</dl>
<dl> <dt><code>valueN</code></dt> <dd>Value of the constant. It can be any legal expression.</dd>
</dl>
<h2>描述</h2>
<p>Creates a constant that can be global or local to the function in which it is declared. Constants follow the same scope rules as variables.</p>
<p>The value of a constant cannot change through re-assignment, and a constant cannot be re-declared. Because of this, although it is possible to declare a constant without initializing it, it would be useless to do so.</p>
<p>A constant cannot share its name with a function or a variable in the same scope.</p>
<h2>浏览器兼容性</h2>
<p>The current implementation of <code>const</code> is a Mozilla-specific extension and is not part of ECMAScript 5. It is supported in Firefox &amp; Chrome (V8). As of Safari 5.1.7 and Opera 12.00, if you define a variable with <code>const</code> in these browsers, you can still change its value later. It is not supported in Internet Explorer 6-9, or in the preview of Internet Explorer 10. The <code>const</code> keyword currently declares the constant in the function scope (like variables declared with <code>var</code>).</p>
<p>FIrefox, as of version 13, will throw a <a href="/zh-cn/JavaScript/Reference/Global_Objects/TypeError" title="zh-cn/JavaScript/Reference/Global_Objects/TypeError">TypeError</a> if you try to redefine a constant. None of the major browsers will produce any notices or errors if you later try to assign another value to a constant. In all these browsers, the return value of such an operation will be that of the new value assigned. Only in Firefox and Chrome (as of version 20) will the reassignment be unsuccessful.</p>
<p><code>const</code> is going to be defined by ECMAScript 6, but with different semantics. Similar to variables declared with the <code>let</code> statement, constants declared with <code>const</code> will be block-scoped.</p>
<h2>例子</h2>
<p>The following example produces the output "<code>a is 7.</code>"</p>
<pre class="brush: js">const a = 7;
console.log("a is " + a + ".");
</pre>
<h2>相关链接</h2>
<ul> <li><code><a href="/zh-cn/JavaScript/Reference/Statements/var" title="zh-cn/Core_JavaScript_1.5_Reference/Statements/var">var</a></code></li> <li><a href="/zh-cn/JavaScript/Reference/Statements/let" title="zh-cn/JavaScript/Reference/Statements/let"><code>let</code></a></li> <li><a href="/zh-cn/JavaScript/Guide/Values,_variables,_and_literals#Constants" title="zh-cn/JavaScript/Guide/Values, Variables, and Literals#Constants">Constants in JavaScript Guide</a></li>
</ul>
<p>{{ languages( { "fr": "fr/R\u00e9f\u00e9rence_de_JavaScript_1.5_Core/Instructions/const", "ja": "ja/Core_JavaScript_1.5_Reference/Statements/const", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Polecenia/const" ,"en":"en/JavaScript/Reference/Statements/const"} ) }}</p>
恢复到这个版本