aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/errors/missing_formal_parameter/index.html
blob: 1528fbe00e30f9b49cd9f5d06efb0ac025d885a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
---
title: 'SyntaxError: missing formal parameter'
slug: Web/JavaScript/Reference/Errors/Missing_formal_parameter
tags:
  - JavaScript
  - 类型错误
  - 错误
  - 错误提示
translation_of: Web/JavaScript/Reference/Errors/Missing_formal_parameter
---
<div>{{jsSidebar("Errors")}}</div>

<h2 id="信息提示">信息提示</h2>

<pre class="syntaxbox">SyntaxError: missing formal parameter (Firefox)
</pre>

<h2 id="错误类型">错误类型</h2>

<p>{{jsxref("SyntaxError")}}</p>

<h2 id="哪里出错了">哪里出错了?</h2>

<p>“形式参数” 是 “函数参数”一种更好的说法。函数声明缺少合法的参数定义。在函数声明中,参数定义必须为标记符({{Glossary("Identifier", "identifiers")}}), 而不是任何类似于数字、字符串或者对象的值。函数声明和函数调用是两个不同的步骤。函数声明中的参数需要使用标记符,而仅仅在函数被呼叫(调用)的时候才可以传入其所需要的值。</p>

<p>{{glossary("JavaScript")}} 中,标记符只能由字母、数字、"$" 以及 "_" 构成,并且不能以数字开头。标记符与<strong>字符串</strong>的区别在于字符串是数据,而标记符属于代码的一部分。</p>

<h2 id="示例">示例</h2>

<p>在构造一个函数的时候,函数参数必须为标记符。下面列举的函数声明都是无效的,因为它们在参数部分使用的是数值:</p>

<pre class="brush: js example-bad">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
</pre>

<p>需要在函数声明中使用标记符:</p>

<pre class="brush: js example-good">function square(number) {
  return number * number;
};

function greet(greeting) {
  return greeting;
};

function log(arg) {
  console.log(arg)
};</pre>

<p>之后可以传入你想要传入的实际参数调用函数:</p>

<pre class="brush: js">square(2); // 4

greet("Howdy"); // "Howdy"

log({obj: "value"}); // Object { obj: "value" }
</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>