Versions

no-func-assign

Disallow reassigning function declarations

Recommended

The "extends": "eslint:recommended" property in a configuration file enables this rule

JavaScript 函数可以被写成 FunctionDeclaration function foo() { ... } 或 FunctionExpression var foo = function() { ... };. 虽然 JavaScript 解释器可能会容忍它,但覆盖/重新分配一个写成 FunctionDeclaration 的函数,往往表明有错误或问题。

function foo() {}
foo = bar;

规则细节

这条规则不允许重新分配 function 的声明。

使用此规则的错误示例:

Open in Playground
/*eslint no-func-assign: "error"*/

function foo() {}
foo = bar;

function foo() {
    foo = bar;
}

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

使用此规则的错误示例,与 JSHint 中的相应规则不同:

Open in Playground
/*eslint no-func-assign: "error"*/

foo = bar;
function foo() {}

使用此规则的正确示例:

Open in Playground
/*eslint no-func-assign: "error"*/

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

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

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

Handled by TypeScript

It is safe to disable this rule when using TypeScript because TypeScript's compiler enforces this check.

Version

This rule was introduced in ESLint v0.0.9.

Resources

更改语言