uTools-Manuals/docs/javascript/Reference/Errors/Illegal_character.html
2019-04-21 11:50:48 +08:00

43 lines
2.4 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">SyntaxError: illegal character (Firefox)
SyntaxError: Invalid or unexpected token (Chrome)
</code></pre>
<h2 id="错误类型">错误类型</h2>
<p><a href="Reference/Global_Objects/SyntaxError" title="SyntaxError 对象代表尝试解析语法上不合法的代码的错误。"><code>SyntaxError</code></a></p>
<h2 id="哪里出错了?">哪里出错了?</h2>
<p>在代码中有非法的或者不期望出现的标记符号出现在不该出现的位置。请使用支持语法高亮功能的编辑器仔细检查你的代码,看看是否存在张冠李戴的情况,比如减号 (<code> - </code>) 与连接符 (<code> </code>) ,或者是英文双引号 (<code> " </code>) 与中文双引号 (<code></code>)。</p>
<h2 id="示例">示例</h2>
<h3 id="错配字符">错配字符</h3>
<p>一些字符看起来会很相像,但是会导致于语法解析器解析代码失败。</p>
<pre><code class="language-js example-bad">“This looks like a string”;
// SyntaxError: illegal character
42 13;
// SyntaxError: illegal character
</code></pre>
<p>下面这样是可以正常运行的:</p>
<pre><code class="language-js example-good">"This is actually a string";
42 - 13;
</code></pre>
<h3 id="遗漏的字符">遗漏的字符</h3>
<p>很容易就会在这里或那里遗漏一些字符。</p>
<pre><code class="language-js example-bad">var colors = ['#000', #333', '#666'];
// SyntaxError: illegal character
</code></pre>
<p>把遗漏的引号给 '#333' 添加上。</p>
<pre><code class="language-js example-good">var colors = ['#000', '#333', '#666'];</code></pre>
<h3 id="隐藏字符">隐藏字符</h3>
<p>当从外部复制粘贴代码的时候,有可能就有非法的隐藏字符的存在,需要引起注意!</p>
<pre><code class="language-js example-bad">var foo = 'bar';
// SyntaxError: illegal character
</code></pre>
<p>当使用文本编辑器如VIM进行探测的时候可以发现这里存在一个零宽空格<a class="external" href="https://en.wikipedia.org/wiki/Zero-width_space" rel="noopener"> (ZWSP) (U+200B)</a> 。</p>
<pre><code class="language-javascript">var foo = 'bar';&lt;200b&gt;</code></pre>
<h2 id="相关内容">相关内容</h2>
<ul>
<li><a href="/en-US/docs/Web/JavaScript/Reference/Lexical_grammar">Lexical grammar</a></li>
</ul>
</article>