★ wanayoo — archive 1999 https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Errors/Missing_formal_parameterNouvelle recherche | Portail wanayoo

MDN’s new design is in Beta! A sneak peek: https://blog.mozilla.org/opendesign/mdns-new-design-beta/

SyntaxError: missing formal parameter

我们的志愿者还没有将这篇文章翻译为 中文 (简体)。加入我们帮助完成翻译!
您也可以阅读此文章的English (US)版。

Message

SyntaxError: missing formal parameter (Firefox)

Error type

SyntaxError

What went wrong?

"Formal parameter" is a fancy way of saying "function parameter". Your function declaration is missing valid parameters. In the declaration of a function, the parameters must be identifiers, not any value like numbers, strings, or objects. Declaring functions and calling functions are two separate steps. Declarations require identifier as parameters, and only when calling (invoking) the function, you provide the values the function should use.

In JavaScript, identifiers can contain only alphanumeric characters (or "$" or "_"), and may not start with a digit. An identifier differs from a string in that a string is data, while an identifier is part of the code.

Examples

Function parameters must be identifiers when setting up a function. All these function declarations fail, as they are providing values for their parameters:

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

You will need to use identifiers in function declarations:

function square(number) {
  return number * number;
};

function greet(greeting) {
  return greeting;
};

function log(arg) {
  console.log(arg)
};

You can then call these functions with the arguments you like:

square(2); // 4

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

log({obj: "value"}); // Object { obj: "value" }

See also

文档标签和贡献者

 此页面的贡献者: YuichiNukiyama, fscholz
 最后编辑者: YuichiNukiyama,