uTools-Manuals/docs/javascript/Reference/Errors/Invalid_const_assignment.html

55 lines
3.5 KiB
HTML
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<article id="wikiArticle">
<div></div>
<h2 id="错误提示">错误提示</h2>
<pre><code class="language-javascript">TypeError: invalid assignment to const "x" (Firefox)
TypeError: Assignment to constant variable. (Chrome)
TypeError: Redeclaration of const 'x' (IE/Edge)
</code></pre>
<h2 id="错误类型">错误类型</h2>
<p><a href="Reference/Global_Objects/TypeError" title="TypeError类型错误 对象用来表示值的类型非预期类型时发生的错误。"><code>TypeError</code></a></p>
<h2 id="哪里出错了?">哪里出错了?</h2>
<p>常量指的是无法在程序正常运行过程中进行修改的值。一方面无法通过重新赋值进行修改,另外一方面也无法进行重新声明。在 JavaScript 中,常量通过关键字 <code><a href="/en-US/docs/Web/JavaScript/Reference/Statements/const">const</a></code> 来声明。</p>
<h2 id="示例">示例</h2>
<h3 id="不合法的二次声明">不合法的二次声明</h3>
<p>在同一作用域内为相同的常量名进行赋值会报错。</p>
<pre><code class="language-js example-bad">const COLUMNS = 80;
// ...
COLUMNS = 120; // TypeError: invalid assignment to const `COLUMNS'</code></pre>
<h3 id="问题修复">问题修复</h3>
<p>修复的方式有很多种。可以根据你想要达到的目的来有针对性地对其进行处理。</p>
<h4 id="重新命名">重新命名</h4>
<p>如果想要声明另一个变量,那么请选择其他名称对其重新命名。原来的常量名在该作用域中已经被占有。</p>
<pre><code class="language-js example-good">const COLUMNS = 80;
const WIDE_COLUMNS = 120;</code></pre>
<h4 id="const_let_or_var"><code>const</code>, <code>let</code> or <code>var</code>?</h4>
<p>如果你的目的不是为了创建一个常量的话,那么就不要使用 const 关键字。可以使用 <code><a href="/en-US/docs/Web/JavaScript/Reference/Statements/let">let</a></code> 关键字来声明一个拥有块作用域的变量,或者使用 <code><a href="/en-US/docs/Web/JavaScript/Reference/Statements/var">var</a> </code>来声明一个全局变量。</p>
<pre><code class="language-js example-good">let columns = 80;
// ...
let columns = 120;
</code></pre>
<h4 id="作用域">作用域</h4>
<p>检查一下作用域是否正确。例如这个常量是否应该出现在当前作用域,还是应该出现在函数内部?</p>
<pre><code class="language-js example-good">const COLUMNS = 80;
function setupBigScreenEnvironment() {
const COLUMNS = 120;
}</code></pre>
<h3 id="const与不可变性"><code>const与不可变性</code></h3>
<p>const 声明语句创建了一个对值的只读引用。这并<strong></strong>意味着它指向的值是不可变的,而是说这个变量标记符不能被再次分配。例如,在值是对象的情况下,引用的对象自身内容依然是可以改变的。不能改变该变量的引用:</p>
<pre><code class="language-js example-bad">const obj = {foo: 'bar'};
obj = {foo: 'baz'}; // TypeError: invalid assignment to const `obj'
</code></pre>
<p>但是可以改变它引用的值的属性: </p>
<pre><code class="language-js example-good">obj.foo = 'baz';
obj; // Object { foo: "baz" }</code></pre>
<h2 id="相关内容">相关内容</h2>
<ul>
<li><code><a href="/en-US/docs/Web/JavaScript/Reference/Statements/const">const</a></code></li>
<li><code><a href="/en-US/docs/Web/JavaScript/Reference/Statements/let">let</a></code></li>
<li><code><a href="/en-US/docs/Web/JavaScript/Reference/Statements/var">var</a></code></li>
</ul>
</article>