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

56 lines
2.9 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.

<article id="wikiArticle">
<div></div>
<h2 id="信息提示">信息提示</h2>
<pre><code class="language-javascript">SyntaxError: missing formal parameter (Firefox)
</code></pre>
<h2 id="错误类型">错误类型</h2>
<p><a href="Reference/Global_Objects/SyntaxError" title="SyntaxError 对象代表尝试解析语法上不合法的代码的错误。"><code>SyntaxError</code></a></p>
<h2 id="哪里出错了">哪里出错了?</h2>
<p>“形式参数” 是 “函数参数”一种更好的说法。函数声明缺少合法的参数定义。在函数声明中,参数定义必须为标记符(<a class="glossaryLink" href="/en-US/docs/Glossary/Identifier" title="identifiers: A sequence of characters in the code that identifies a variable, function, or property.">identifiers</a>), 而不是任何类似于数字、字符串或者对象的值。函数声明和函数调用是两个不同的步骤。函数声明中的参数需要使用标记符,而仅仅在函数被呼叫(调用)的时候才可以传入其所需要的值。</p>
<p><a class="glossaryLink" href="/en-US/docs/Glossary/JavaScript" title="JavaScript: JavaScript (JS) is a programming language mostly used to dynamically script webpages on the client side, but it is also often utilized on the server-side, using packages such as Node.js.">JavaScript</a> 中,标记符只能由字母、数字、"$" 以及 "_" 构成,并且不能以数字开头。标记符与<strong>字符串</strong>的区别在于字符串是数据,而标记符属于代码的一部分。</p>
<h2 id="示例">示例</h2>
<p>在构造一个函数的时候,函数参数必须为标记符。下面列举的函数声明都是无效的,因为它们在参数部分使用的是数值:</p>
<pre><code class="language-js example-bad highlight:[1,6,11]">function square(3) {
return number * number;
};
// SyntaxError: missing formal parameter
function greet("Howdy") {
return greeting;
};
// SyntaxError: missing formal parameter
function log({ obj: "value"}) {
console.log(arg)
};
// SyntaxError: missing formal parameter
</code></pre>
<p>需要在函数声明中使用标记符:</p>
<pre><code class="language-js example-good highlight:[1,5,9]">function square(number) {
return number * number;
};
function greet(greeting) {
return greeting;
};
function log(arg) {
console.log(arg)
};</code></pre>
<p>之后可以传入你想要传入的实际参数调用函数:</p>
<pre><code class="language-javascript">square(2); // 4
greet("Howdy"); // "Howdy"
log({obj: "value"}); // Object { obj: "value" }
</code></pre>
<h2 id="相关内容">相关内容</h2>
<ul>
<li>Other errors regarding formal parameters:
<ul>
<li><a href="/en-US/docs/Web/JavaScript/Reference/Errors/Malformed_formal_parameter">SyntaxError: Malformed formal parameter</a></li>
<li><a href="/en-US/docs/Web/JavaScript/Reference/Errors/Redeclared_parameter">SyntaxError: redeclaration of formal parameter "x"</a></li>
</ul>
</li>
</ul>
</article>