Index

no-func-assign

禁止重新分配 function 声明

Recommended

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

JavaScript 函数可以写成函数声明 function foo() { ... } 或函数表达式 const foo = function() { ... };。虽然 JavaScript 解释器可能会容忍这样做,但重写/重新分配用函数声明写的函数往往表明存在错误或问题。

🌐 JavaScript functions can be written as a FunctionDeclaration function foo() { ... } or as a FunctionExpression const foo = function() { ... };. While a JavaScript interpreter might tolerate it, overwriting/reassigning a function written as a FunctionDeclaration is often indicative of a mistake or issue.

function foo() {}
foo = bar;

规则详情

🌐 Rule Details

此规则不允许重新分配 function 声明。

🌐 This rule disallows reassigning function declarations.

此规则的错误代码示例:

🌐 Examples of incorrect code for this rule:

在线运行
/*eslint no-func-assign: "error"*/

function foo() {}
foo = bar;

function baz() {
    baz = bar;
}

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

与 JSHint 中对应的规则不同,该规则的 错误 代码示例:

🌐 Examples of incorrect code for this rule, unlike the corresponding rule in JSHint:

在线运行
/*eslint no-func-assign: "error"*/

foo = bar;
function foo() {}

符合此规则的正确代码示例:

🌐 Examples of correct code for this rule:

在线运行
/*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.
}

选项

🌐 Options

此规则没有选项。

🌐 This rule has no options.

由 TypeScript 处理

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

版本

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

资源