SyntaxError: missing formal parameter - JavaScript 编辑

The JavaScript exception "missing formal parameter" occurs when your function declaration is missing valid parameters.

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

Provide proper function parameters

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:134 次

字数:3313

最后编辑:7年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文