no-func-assign
禁止重新分配 function
声明
在 配置文件 中使用来自 @eslint/js
的 recommended
配置可以启用此规则
JavaScript 函数可以写成 FunctionDeclaration function foo() { ... }
或 FunctionExpression var foo = function() { ... };
。虽然 JavaScript 解释器可能会容忍它,但覆盖/重新分配编写为 FunctionDeclaration 的函数通常表明存在错误或问题。
¥JavaScript functions can be written as a FunctionDeclaration function foo() { ... }
or as a FunctionExpression var 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;
}
var 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"*/
var foo = function () {}
foo = bar;
function baz(baz) { // `baz` is shadowed.
baz = bar;
}
function qux() {
var qux = bar; // `qux` is shadowed.
}
由 TypeScript 处理
使用 TypeScript 时禁用此规则是安全的,因为 TypeScript 的编译器强制执行此检查。
版本
此规则是在 ESLint v0.0.9 中引入。