no-func-assign
禁止重新赋值 function
声明
✅ 推荐
在 配置文件 中使用来自 @eslint/js
的 recommended
配置启用此规则
JavaScript 函数可以写成 FunctionDeclaration function foo() { ... }
或 FunctionExpression const foo = function() { ... };
。虽然 JavaScript 解释器可能容忍它,但覆盖/重新赋值写为 FunctionDeclaration 的函数通常表示错误或问题。
function foo() {}
foo = bar;
规则详情
此规则禁止重新赋值 function
声明。
此规则的错误代码示例
在 Playground 中打开
/*eslint no-func-assign: "error"*/
function foo() {}
= bar;
function baz() {
= bar;
}
let a = function hello() {
= 123;
};
此规则的错误代码示例,与 JSHint 中相应的规则不同
在 Playground 中打开
/*eslint no-func-assign: "error"*/
= 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 中引入。