no-inner-declarations

禁止嵌套块中的变量或 function 声明

配置文件 中的 "extends": "eslint:recommended" 属性启用了该规则

在 JavaScript 中,在 ES6 之前,函数声明只允许在程序的第一级或另一个函数的主体中,尽管解析器有时 在别处错误地接受它们。这仅适用于函数声明;命名或匿名函数表达式可以出现在允许表达式的任何地方。

// Good
function doSomething() { }

// Bad
if (test) {
    function doSomethingElse () { }
}

function anotherThing() {
    var fn;

    if (test) {

        // Good
        fn = function expression() { };

        // Bad
        function declaration() { }
    }
}

变量声明可以在语句可以到达的任何地方进行,甚至可以嵌套在其他块的深处。由于变量提升,这通常是不可取的,并且将声明移动到程序或函数体的根可以增加清晰度。请注意,块绑定 (let, const) 没有被吊起,因此它们不受此规则的影响。

/*eslint-env es6*/

// Good
var foo = 42;

// Good
if (foo) {
    let bar1;
}

// Bad
while (test) {
    var bar2;
}

function doSomething() {
    // Good
    var baz = true;

    // Bad
    if (baz) {
        var quux;
    }
}

规则详情

该规则要求函数声明和变量声明(可选)位于程序的根目录中,或位于函数主体的根目录中,或位于类静态块主体的根目录中。

选项

ZmISekGbcAWtmV//14mcrMbiZ99KsErc/LlCDwgRoycwd1PymkiMp4tocaChTFg+

    EPH/ukSK0JR7ZOLqW8cX1ZMNJhiaOwE4pdeDyvDF0b8Va6hsYuhaw1s+mGOe1izzqmWR+X8HQdzGVnYQymJBflOqx+VE4jM4pK7C0suXaJDvrTahiyj/i459fFjNsn5F+39K/Blkt+XU4aqnWl0dbJyY1CHqkZmnT1iYsJksfFzcQPSIAxytiN4JV0pgiPNYR0zfg255CtQJuxuYxfDsCZ3a++EV/g4I+fdbzPFlq5sSup9vfVf/XYjT5syqoLlO0fWskadpc9lPq9Un3E/N2w==

functions

6OjE+A1hvEglXN51x/s/8hn2cKAr4Vz50xOLwniMDIPClU1b9MuXpuV5NJBpoxEFM2X4HXkmHhfN76FlbylfEeBsH1tEnooMguSA27ioFmA=

/*eslint no-inner-declarations: "error"*/

if (test) {
    function doSomething() { }
}

function doSomethingElse() {
    if (test) {
        function doAnotherThing() { }
    }
}

if (foo) function f(){}

class C {
    static {
        if (test) {
            function doSomething() { }
        }
    }
}

6OjE+A1hvEglXN51x/s/8hn2cKAr4Vz50xOLwniMDIPClU1b9MuXpuV5NJBpoxEFUpkXzXCko4xNkAyyQZdUl2xJNrrHdLLCkARffntXe/A=

/*eslint no-inner-declarations: "error"*/

function doSomething() { }

function doSomethingElse() {
    function doAnotherThing() { }
}

class C {
    static {
        function doSomething() { }
    }
}

if (test) {
    asyncCall(id, function (err, data) { });
}

var fn;
if (test) {
    fn = function fnExpression() { };
}

if (foo) var a;

both

7xDYtjDnSQq8pZ502SNO6sn2caSS3yp7s2B/Z+6sfgrFG+Zylvm/BbINg+PKK9wTlO8fBc/sJOWSNY4vLecEBfoheMf/YT+/aklOBkNmKds=

/*eslint no-inner-declarations: ["error", "both"]*/

if (test) {
    var foo = 42;
}

function doAnotherThing() {
    if (test) {
        var bar = 81;
    }
}

if (foo) var a;

if (foo) function f(){}

class C {
    static {
        if (test) {
            var something;
        }
    }
}

7xDYtjDnSQq8pZ502SNO6sn2caSS3yp7s2B/Z+6sfgp4nf27X1UOxvFn5asSrl7Nz0R9L9ep3Y9o6Xli2TDfV5MfxAtMpE9itaFrxDcjFqA=

/*eslint no-inner-declarations: ["error", "both"]*/

var bar = 42;

if (test) {
    let baz = 43;
}

function doAnotherThing() {
    var baz = 81;
}

class C {
    static {
        var something;
    }
}

何时不使用

9nbQJmuYJtwKhVCLSrqlt1P3B0yhdyOddliplv97SX0P3lXGpJuLAaHUgHmWbEvr2K39OOZepLr6Vw63IGwfC+4RbrnGjxxJN2ZpVYcHpKL1N8YWaV/GTXRgRRxK/h9iXoIc7X34IleN5LZLg6EcxSRrGiHxQbkECd/xqyamx1C3iVqzqQ0RTDLOtB+LmZw6glYMa1U171nLJ0S9FnFT/BERMC3TUTSS4eBc2AueG3sgy0Z4blIXiup7Qgcn1B8C2w57lV49VdjSMVY/6BT7hZolku02BwOK4r2vbLBmqirtRTGoAE6d0czJXOGheCGsw4wB3yfbncru87ASdJRXdSJLzGoMC4Au3UGMYhGfp0GqTeVdOeEz4UdH+SouZFXxshjq1JiVyEfWCdogKs1s8nyOBdPXlLe/d6jMZ8xselp628lDM3/Rai7GmasA/4bOoSuQeyBeQWCRRdvqF36hszstrvyGqvDSK6rNs5y/sodWyigvjuULvuAyMV9SybWp