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

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">SyntaxError: function statement requires a name [Firefox]
SyntaxError: 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><a href="/en-US/docs/Web/JavaScript/Reference/Statements/function">函数声明</a>需要提供函数名称。你需要检查函数是如何定义的,是否需要为其提供名称,出现问题的函数是否需要声明为函数表达式或立即调用函数表达式(<a class="glossaryLink" href="/en-US/docs/Glossary/IIFE" title="IIFE: An IIFE (Immediately Invoked Function Expression) is a JavaScript function that runs as soon as it is defined.">IIFE</a>),以及函数在上下文环境中出现的位置是否正确。</p>
<h2 id="示例">示例</h2>
<h3 id="语句与表达式">语句与表达式</h3>
<p><a href="/en-US/docs/Web/JavaScript/Reference/Statements/function">函数语句</a>(或函数声明)需要命名,以下写法是不正确的:</p>
<pre><code class="language-js example-bad">function () {
return 'Hello world';
}
// SyntaxError: function statement requires a name
</code></pre>
<p>你可以使用<a href="/en-US/docs/Web/JavaScript/Reference/Operators/function">函数表达式</a>(赋值)来代替:</p>
<pre><code class="language-js example-good">var greet = function() {
return 'Hello world';
};</code></pre>
<p>者是你想将其作为立即调用函数表达式(<a class="external" href="https://en.wikipedia.org/wiki/Immediately-invoked_function_expression" rel="noopener">IIFE</a>Immediately Invoked Function Expression也就是定义后立即执行的函数。在这种情况下你需要用到更多的括号</p>
<pre><code class="language-js example-good">(function () {
})();</code></pre>
<h3 id="标号函数(Labeled_functions)">标号函数(Labeled functions)</h3>
<p>你使用函数标号(<a href="/en-US/docs/Web/JavaScript/Reference/Statements/label">labels)</a>的时候, 也需要在关键词 <code>function</code> 后面提供一个函数名称. 这样的代码是不能运行的:</p>
<pre><code class="language-js example-bad">function Greeter() {
german: function () {
return "Moin";
}
}
// SyntaxError: function statement requires a name
</code></pre>
<p>这个例子可以正常运行:</p>
<pre><code class="language-js example-good">function Greeter() {
german: function g() {
return "Moin";
}
}</code></pre>
<h3 id="对象方法">对象方法</h3>
<p>如果你想创建创建一个对象方法那么需要首先创建一个对象。以下语法function 关键字后面没有提供名称)是合法的:</p>
<pre><code class="language-js example-good">var greeter = {
german: function () {
return "Moin";
}
};</code></pre>
<h3 id="回调函数的语法">回调函数的语法</h3>
<p>另外,如果使用到了回调函数,那么检查一下语法是否正确。大括号与逗号很容易使情况变糟。</p>
<pre><code class="language-js example-bad">promise.then(
function() {
console.log("success");
});
function() {
console.log("error");
}
// SyntaxError: function statement requires a name
</code></pre>
<p>正确的形式应该是这样的:</p>
<pre><code class="language-json example-good">promise.then(
function() {
console.log("success");
},
function() {
console.log("error");
}
);
</code></pre>
<h2 id="相关内容">相关内容</h2>
<ul>
<li><a href="/en-US/docs/Web/JavaScript/Guide/Functions">Functions in the JavaScript Guide</a></li>
<li><a href="/en-US/docs/Web/JavaScript/Reference/Statements/function">function statement</a></li>
<li><a href="/en-US/docs/Web/JavaScript/Reference/Operators/function">function expression</a></li>
<li><a class="external" href="https://en.wikipedia.org/wiki/Immediately-invoked_function_expression" rel="noopener">IIFE</a></li>
<li><a href="/en-US/docs/Web/JavaScript/Reference/Statements/label">label</a></li>
</ul>
</article>