mirror of
https://github.com/fofolee/uTools-Manuals.git
synced 2026-02-26 17:11:20 +08:00
语法高亮,滚动条美化,设置页面调整
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
<article id="wikiArticle">
|
||||
<div></div>
|
||||
<h2 id="错误提示">错误提示</h2>
|
||||
<pre class="syntaxbox">TypeError: invalid assignment to const "x" (Firefox)
|
||||
<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)
|
||||
</pre>
|
||||
</code></pre>
|
||||
<h2 id="错误类型">错误类型</h2>
|
||||
<p><a href="Reference/Global_Objects/TypeError" title="TypeError(类型错误) 对象用来表示值的类型非预期类型时发生的错误。"><code>TypeError</code></a></p>
|
||||
<h2 id="哪里出错了?">哪里出错了?</h2>
|
||||
@@ -12,40 +12,40 @@ TypeError: Redeclaration of const 'x' (IE/Edge)
|
||||
<h2 id="示例">示例</h2>
|
||||
<h3 id="不合法的二次声明">不合法的二次声明</h3>
|
||||
<p>在同一作用域内为相同的常量名进行赋值会报错。</p>
|
||||
<pre class="brush: js example-bad">const COLUMNS = 80;
|
||||
<pre><code class="language-js example-bad">const COLUMNS = 80;
|
||||
|
||||
// ...
|
||||
|
||||
COLUMNS = 120; // TypeError: invalid assignment to const `COLUMNS'</pre>
|
||||
COLUMNS = 120; // TypeError: invalid assignment to const `COLUMNS'</code></pre>
|
||||
<h3 id="问题修复">问题修复</h3>
|
||||
<p>修复的方式有很多种。可以根据你想要达到的目的来有针对性地对其进行处理。</p>
|
||||
<h4 id="重新命名">重新命名</h4>
|
||||
<p>如果想要声明另一个变量,那么请选择其他名称对其重新命名。原来的常量名在该作用域中已经被占有。</p>
|
||||
<pre class="brush: js example-good">const COLUMNS = 80;
|
||||
const WIDE_COLUMNS = 120;</pre>
|
||||
<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 class="brush: js example-good">let columns = 80;
|
||||
<pre><code class="language-js example-good">let columns = 80;
|
||||
|
||||
// ...
|
||||
|
||||
let columns = 120;
|
||||
</pre>
|
||||
</code></pre>
|
||||
<h4 id="作用域">作用域</h4>
|
||||
<p>检查一下作用域是否正确。例如这个常量是否应该出现在当前作用域,还是应该出现在函数内部?</p>
|
||||
<pre class="brush: js example-good">const COLUMNS = 80;
|
||||
<pre><code class="language-js example-good">const COLUMNS = 80;
|
||||
|
||||
function setupBigScreenEnvironment() {
|
||||
const COLUMNS = 120;
|
||||
}</pre>
|
||||
}</code></pre>
|
||||
<h3 id="const与不可变性"><code>const与不可变性</code></h3>
|
||||
<p>const 声明语句创建了一个对值的只读引用。这并<strong>不</strong>意味着它指向的值是不可变的,而是说这个变量标记符不能被再次分配。例如,在值是对象的情况下,引用的对象自身内容依然是可以改变的。不能改变该变量的引用:</p>
|
||||
<pre class="brush: js example-bad">const obj = {foo: 'bar'};
|
||||
<pre><code class="language-js example-bad">const obj = {foo: 'bar'};
|
||||
obj = {foo: 'baz'}; // TypeError: invalid assignment to const `obj'
|
||||
</pre>
|
||||
</code></pre>
|
||||
<p>但是可以改变它引用的值的属性: </p>
|
||||
<pre class="brush: js example-good">obj.foo = 'baz';
|
||||
obj; // Object { foo: "baz" }</pre>
|
||||
<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>
|
||||
|
||||
Reference in New Issue
Block a user