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

78 lines
4.2 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">Firefox:
句法错误: "use strict" 不允许在带默认参数的函数中
句法错误: "use strict" 不允许在带rest参数的函数中
句法错误: "use strict" 不允许在带解构参数的函数中
Chrome:
句法错误: 非法的'use strict'指令,在带有非简单参数列表的函数中
</code></pre>
<h2 id="错误类型">错误类型</h2>
<p><a href="Reference/Global_Objects/SyntaxError" title="SyntaxError 对象代表尝试解析语法上不合法的代码的错误。"><code>SyntaxError</code></a>.</p>
<h2 id="哪里出错了">哪里出错了?</h2>
<p><code><font face="Open Sans, Arial, sans-serif">在函数顶部直接写了 </font>"use strict"</code> ,而该函数拥有以下的参数其中之一:</p>
<ul>
<li><a href="Reference/Functions/Default_parameters" title="函数默认参数允许在没有值或undefined被传入时使用默认形参。">默认参数</a></li>
<li><a href="Reference/Functions/rest_parameters" title="剩余参数语法允许我们将一个不定数量的参数表示为一个数组。">剩余参数</a></li>
<li><a href="Reference/Operators/Destructuring_assignment" title="解构赋值语法是一个 Javascript 表达式,这使得可以将值从数组或属性从对象提取到不同的变量中。">解构赋值</a></li>
</ul>
<p>根据ECMAScript规范不允许在这些函数的顶部使用“use strict”指令。</p>
<h2 id="示例">示例</h2>
<h3 id="函数语句">函数语句</h3>
<p>在这种情况下函数sum具有默认参数a = 1和b = 2</p>
<pre><code class="language-js example-bad">function sum(a=1, b=2) {
// SyntaxError: "use strict" not allowed in function with default parameter
"use strict";
return a + b;
}
</code></pre>
<p>如果这个函数应该处于 <a href="/en-US/docs/Web/JavaScript/Reference/Strict_mode">strict mode</a>,并且整个脚本或封装函数也可以在严格模式下,可以移动 "use strict" 指令到函数之外:</p>
<pre><code class="language-js example-good">"use strict";
function sum(a=1, b=2) {
return a + b;
}
</code></pre>
<h3 id="函数表达式">函数表达式</h3>
<p>函数表达式可以使用另一种解决方法:</p>
<pre><code class="language-js example-bad">var sum = function sum([a, b]) {
// SyntaxError: "use strict" not allowed in function with destructuring parameter
"use strict";
return a + b;
};
</code></pre>
<p>这可以转换为以下表达式:</p>
<pre><code class="language-js example-good">var sum = (function() {
"use strict";
return function sum([a, b]) {
return a + b;
};
})();
</code></pre>
<h3 id="箭头函数">箭头函数</h3>
<p>如果箭头函数需要访问 <code>this</code>,则可以将箭头函数作为封闭函数来使用:</p>
<pre><code class="language-js example-bad">var callback = (...args) =&gt; {
// SyntaxError: "use strict" not allowed in function with rest parameter
"use strict";
return this.run(args);
};
</code></pre>
<p>这可以转换为以下表达式:</p>
<pre><code class="language-js example-good">var callback = (() =&gt; {
"use strict";
return (...args) =&gt; {
return this.run(args);
};
})();
</code></pre>
<h2 id="也可以看看">也可以看看</h2>
<ul>
<li><a href="Reference/Strict_mode" title="如果你想改变你的代码让其工作在具有限制性JavaScript环境中请参阅转换成严格模式。">Strict mode</a></li>
<li><a href="Reference/Statements/function" title="函数声明定义一个具有指定参数的函数。">函数语句</a></li>
<li><a href="Reference/Operators/function" title="function 关键字可以用来在一个表达式中定义一个函数。">函数表达式</a></li>
<li><a href="Reference/Functions/Default_parameters" title="函数默认参数允许在没有值或undefined被传入时使用默认形参。">默认参数</a></li>
<li><a href="Reference/Functions/rest_parameters" title="剩余参数语法允许我们将一个不定数量的参数表示为一个数组。">剩余参数</a></li>
<li><a href="Reference/Operators/Destructuring_assignment" title="解构赋值语法是一个 Javascript 表达式,这使得可以将值从数组或属性从对象提取到不同的变量中。">解构参数</a></li>
</ul>
</article>