版本

no-func-assign

禁止重新赋值 function 声明

推荐

配置文件 中使用来自 @eslint/jsrecommended 配置启用此规则

JavaScript 函数可以写成 FunctionDeclaration function foo() { ... } 或 FunctionExpression const foo = function() { ... };。虽然 JavaScript 解释器可能容忍它,但覆盖/重新赋值写为 FunctionDeclaration 的函数通常表示错误或问题。

function foo() {}
foo = bar;

规则详情

此规则禁止重新赋值 function 声明。

此规则的错误代码示例

在 Playground 中打开
/*eslint no-func-assign: "error"*/

function foo() {}
foo = bar;

function baz() {
    baz = bar;
}

let a = function hello() {
  hello = 123;
};

此规则的错误代码示例,与 JSHint 中相应的规则不同

在 Playground 中打开
/*eslint no-func-assign: "error"*/

foo = bar;
function foo() {}

此规则的正确代码示例

在 Playground 中打开
/*eslint no-func-assign: "error"*/

let foo = function () {}
foo = bar;

function baz(baz) { // `baz` is shadowed.
    baz = bar;
}

function qux() {
    const qux = bar;  // `qux` is shadowed.
}

由 TypeScript 处理

当使用 TypeScript 时,禁用此规则是安全的,因为 TypeScript 的编译器会强制执行此检查。

版本

此规则在 ESLint v0.0.9 中引入。

资源

更改语言