no-use-before-define

在定义之前禁止使用变量

在 JavaScript 中,在 ES6 之前,变量和函数声明被提升到作用域的顶部,因此可以在代码中的正式声明之前使用标识符。这可能会令人困惑,有些人认为最好在使用变量和函数之前始终声明它们。

在 ES6 中,块级绑定(letconst)引入了一个 "temporal dead zone",其中一个 ReferenceError 将被抛出,任何尝试在声明之前访问该变量。

规则详情

当遇到对尚未声明的标识符的引用时,此规则将发出警告。

此规则的错误代码示例:

/*eslint no-use-before-define: "error"*/

alert(a);
var a = 10;

f();
function f() {}

function g() {
    return b;
}
var b = 1;

{
    alert(c);
    let c = 1;
}

{
    class C extends C {}
}

{
    class C {
        static x = "foo";
        [C.x]() {}
    }
}

{
    const C = class {
        static x = C;
    }
}

{
    const C = class {
        static {
            C.x = "foo";
        }
    }
}

export { foo };
const foo = 1;

FphvHt5MK/G5o4JKMpo8wFZScNX7kGyQzE3xqEeYlFcVQkbgr+qufrIGgDR/9BRp

/*eslint no-use-before-define: "error"*/

var a;
a = 10;
alert(a);

function f() {}
f(1);

var b = 1;
function g() {
    return b;
}

{
    let c;
    c++;
}

{
    class C {
        static x = C;
    }
}

{
    const C = class C {
        static x = C;
    }
}

{
    const C = class {
        x = C;
    }
}

{
    const C = class C {
        static {
            C.x = "foo";
        }
    }
}

const foo = 1;
export { foo };

选项

{
    "no-use-before-define": ["error", {
        "functions": true,
        "classes": true,
        "variables": true,
        "allowNamedExports": false
    }]
}
    Fv/JBiFl3YkG8MzVAMj5NNJkiTBgXRFiiLUku/Pj0/JU8JwNWQr+7APmPFZKd00sYLTMU2k0R/DW6GXAOu00vshLzwUzrQctMO51rNWVFhhmFzpYU5Ebtc7qpyCYBCgCyXQMqB7HSvZ0fKIjS0CPXJUI8AqKv7BYZIvjDUFI5cK9D5GUDkRAWQxvFqyk8zUgRhgkj8fp7BsSL889CWMXIBM0ahhnJBQrh8v3siQeGi3EGDZB+AKTV/+pG+ozLghwtIN+rX8l6flQvlEDe6IdlMzoUUvbwK5G8Kq+sivcVdAMQY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SQHkATg4k2KjL+0OSzTTrMzYMfpi7OeZi648CH00kjNN8Izh2YBRB6QcFRbMTeKioRouWsdMUEdeMwYLbu0EsCDMrJHdWK8VlbVP3+XTpw7W8Jr+qhf7UIkSuL7Day0JQB9W3WdFPlQnRgPd6sLWtwEzmnbg1PeHXaBwme5XK59HF3c+xqJknJkJK7FdUHHjeuCtrAfB5MnK1Z/0B2yi7N0t8ocsaED6YacxeQscTr16RwPfAKDRKEu58ttWmmF2W9nrX0uFm7+IA6wwmAbM4ZVGhRioWbj5jkZw3cuddm6rANkLkLjDCMytfN967gPpyswvktbZV8/LAW1KKOZaG6fIFjoQUXNFAzXe2B4f3CXsquzLBJY70QoZ9U6ErnsG5x4q7s7zMLz3F5NXwf8w48Q5RLHpE3q7mSO0Xim+kg==

C6MC8Sv3ufEIeD9qss7fzDUhpr4ozavyy0mYF3IrRN7F+Ku4q00JvfbMeSaYnR7zNcndRrnqec7TkVad9CMK6aFOAxG3rTmmb5VuKT6KKStktULT5bFFx2ik7E48sdTdRSZdckMTiNDNt6vFBmKipx+BULIw7MU30vyAeP44f2pW8pQpVrVO4msAQ2ChHnowTa25kvGr6TqaI7c2K3L6thfrNkoj4xSVMGCADwFmZHchN8o/l5FsFfJ4X+a/xfiZETC9LfZm37E+Ts6+1Vdfkw==

functions

NJ26fpTgBvDTprPhDzA+I1gHRsTMGJVudGt523f3mJKgY3hXZZZty3NjDAGZshFkuVQ+8LXK+xgMnpiRzsQa9hupM5LyBcAn6iiwUdgEy/w=

/*eslint no-use-before-define: ["error", { "functions": false }]*/

f();
function f() {}

7L++vv3JpIgasIIiqfz/nSxcfjM0guhyJFZFXXHX2jGMz2hh4pvdxjTXF8/xD9VkALv0CLfAjlWhWh2NNwcDyGZnf1IWiPQAmTzmcoUfdmkMg2ofikBRUeGBx6RiypZ4xS4el8yXq+QIdvy6vY4LHCOAt442Wa+Ceks9aLi4T0Hae6kIXX8+XfAJjckPJ/o0PZXY9LBk13oOVfEXpq/rIs3aBLCzip9ovtoGPsOWpTQ=

classes

Zw5lkuOijDI/D7oVAHNxzwmXentDer806LP4aIF8hkqFf54LjdCiO0uloirwbliNjMkNG+s/jEfZNeoFYrmRfkVtlm6kZt3qFCGTYeXzL6c=

/*eslint no-use-before-define: ["error", { "classes": false }]*/

new A();
class A {
}

{
    class C extends C {}
}

{
    class C extends D {}
    class D {}
}

{
    class C {
        static x = "foo";
        [C.x]() {}
    }
}

{
    class C {
        static {
            new D();
        }
    }
    class D {}
}

Zw5lkuOijDI/D7oVAHNxzwmXentDer806LP4aIF8hkqfqEAkIbJbweTGODvIcAqGNGnKb8ihFR0KzLEvJpK4e/KesO3XK95d1hbTGwQVJd4=

/*eslint no-use-before-define: ["error", { "classes": false }]*/

function foo() {
    return new A();
}

class A {
}

variables

vgIstLKghofjYW6GfI5ACYk4OoWuitxaUtBIY6mCuixCrLBpXx3kb3leFUUvzTOyEUyE5vVNz9v9tfLRV1ndT390cFogSkFWQF0LNAz8Kxw=

/*eslint no-use-before-define: ["error", { "variables": false }]*/

console.log(foo);
var foo = 1;

f();
const f = () => {};

g();
const g = function() {};

{
    const C = class {
        static x = C;
    }
}

{
    const C = class {
        static x = foo;
    }
    const foo = 1;
}

{
    class C {
        static {
            this.x = foo;
        }
    }
    const foo = 1;
}

vgIstLKghofjYW6GfI5ACYk4OoWuitxaUtBIY6mCuiyBdDxLV/Pujh1oaF0FaaSI5WfNCUWdqdd3zQ5C0+5EAgcz1tCAfZ66NF8gWHQyIJs=

/*eslint no-use-before-define: ["error", { "variables": false }]*/

function baz() {
    console.log(foo);
}
var foo = 1;

const a = () => f();
function b() { return f(); }
const c = function() { return f(); }
const f = () => {};

const e = function() { return g(); }
const g = function() {}

{
    const C = class {
        x = foo;
    }
    const foo = 1;
}

allowNamedExports

o+zHIrwE2mmOHPYVk1F0RRqWoY6jE8TotpjKUwaMAsepikYAgpAyIla2F0JQUQ3h31oxuPyiiZVm551t0plV4HgpWij5A82fhbUfrPHvVbU=

/*eslint no-use-before-define: ["error", { "allowNamedExports": true }]*/

export { a, b, f, C };

const a = 1;

let b;

function f () {}

class C {}

o+zHIrwE2mmOHPYVk1F0RRqWoY6jE8TotpjKUwaMAsepikYAgpAyIla2F0JQUQ3hyk0Yus/I1qPR17QCkBuSAhIghG2ENFzpsNgOhVL/DlI=

/*eslint no-use-before-define: ["error", { "allowNamedExports": true }]*/

export default a;
const a = 1;

const b = c;
export const c = 1;

export function foo() {
    return d;
}
const d = 1;