函数表达式

function 关键字可用于在表达式内定义函数。

¥The function keyword can be used to define a function inside an expression.

你还可以使用 function 声明箭头语法 定义函数。

¥You can also define functions using the function declaration or the arrow syntax.

Try it

语法

¥Syntax

js
function (param0) {
  statements
}
function (param0, param1) {
  statements
}
function (param0, param1, /* …, */ paramN) {
  statements
}

function name(param0) {
  statements
}
function name(param0, param1) {
  statements
}
function name(param0, param1, /* …, */ paramN) {
  statements
}

注意:表达式语句 不能以关键字 function 开头,以避免与 function 声明 产生歧义。function 关键字仅在出现在无法接受语句的上下文中时才开始表达式。

¥Note: An expression statement cannot begin with the keyword function to avoid ambiguity with a function declaration. The function keyword only begins an expression when it appears in a context that cannot accept statements.

参数

¥Parameters

name Optional

函数名称。可以省略,在这种情况下该函数是匿名的。该名称仅是函数体的局部名称。

paramN Optional

函数的形式参数的名称。有关参数的语法,请参阅 函数参考

statements Optional

构成函数体的语句。

描述

¥Description

function 表达式与 function 声明 非常相似,并且具有几乎相同的语法。function 表达式和 function 声明之间的主要区别在于函数名称,在 function 表达式中可以省略函数名称以创建匿名函数。function 表达式可以用作 IIFE(立即调用函数表达式),一旦定义就立即运行。另请参阅有关 functions 的章节以获取更多信息。

¥A function expression is very similar to, and has almost the same syntax as, a function declaration. The main difference between a function expression and a function declaration is the function name, which can be omitted in function expressions to create anonymous functions. A function expression can be used as an IIFE (Immediately Invoked Function Expression) which runs as soon as it is defined. See also the chapter about functions for more information.

函数表达式提升

¥Function expression hoisting

函数声明 不同,JavaScript 中的函数表达式不会提升。在创建函数表达式之前不能使用它们:

¥Function expressions in JavaScript are not hoisted, unlike function declarations. You can't use function expressions before you create them:

js
console.log(notHoisted); // undefined
// Even though the variable name is hoisted,
// the definition isn't. so it's undefined.
notHoisted(); // TypeError: notHoisted is not a function

var notHoisted = function () {
  console.log("bar");
};

命名函数表达式

¥Named function expression

如果要在函数体内引用当前函数,则需要创建一个命名函数表达式。该名称仅适用于函数体(作用域)。这可以避免使用已弃用的 arguments.callee 属性来递归调用该函数。

¥If you want to refer to the current function inside the function body, you need to create a named function expression. This name is then local only to the function body (scope). This avoids using the deprecated arguments.callee property to call the function recursively.

js
const math = {
  factit: function factorial(n) {
    console.log(n);
    if (n <= 1) {
      return 1;
    }
    return n * factorial(n - 1);
  },
};

math.factit(3); //3;2;1;

如果函数表达式被命名,则函数的 name 属性将设置为该名称,而不是从语法推断的隐式名称(例如函数分配给的变量)。

¥If a function expression is named, the name property of the function is set to that name, instead of the implicit name inferred from syntax (such as the variable the function is assigned to).

与声明不同,函数表达式的名称是只读的。

¥Unlike declarations, the name of the function expressions is read-only.

js
function foo() {
  foo = 1;
}
foo();
console.log(foo); // 1
(function foo() {
  foo = 1; // TypeError: Assignment to constant variable.
})();

示例

¥Examples

使用函数表达式

¥Using function expression

以下示例定义了一个未命名函数并将其分配给 x。该函数返回其参数的平方:

¥The following example defines an unnamed function and assigns it to x. The function returns the square of its argument:

js
const x = function (y) {
  return y * y;
};

使用函数作为回调

¥Using a function as a callback

更常见的是用作 callback

¥More commonly it is used as a callback:

js
button.addEventListener("click", function (event) {
  console.log("button is clicked!");
});

使用立即调用函数表达式 (IIFE)

¥Using an Immediately Invoked Function Expression (IIFE)

创建并调用匿名函数:

¥An anonymous function is created and called:

js
(function () {
  console.log("Code runs!");
})();

// or

!function () {
  console.log("Code runs!");
}();

规范

Specification
ECMAScript Language Specification
# sec-function-definitions

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看