Index

no-implicit-globals

不允许在全局作用域内声明

最好避免用本应仅在脚本中使用的变量“污染”全局作用域。

🌐 It is the best practice to avoid ‘polluting’ the global scope with variables that are intended to be local to the script.

从脚本创建的全局变量可能与从另一个脚本创建的全局变量产生名称冲突,这通常会导致运行时错误或意外行为。

🌐 Global variables created from a script can produce name collisions with global variables created from another script, which will usually lead to runtime errors or unexpected behavior.

此规则不允许以下内容:

🌐 This rule disallows the following:

  • 在全局作用域内创建一个或多个变量的声明。
  • 全局变量泄漏。
  • 只读全局变量的重新声明和对只读全局变量的赋值。

有一种明确的方法可以在需要时通过分配给全局对象的属性来创建全局变量。

🌐 There is an explicit way to create a global variable when needed, by assigning to a property of the global object.

此规则主要对浏览器脚本有用。ES 模块和 CommonJS 模块中的顶层声明会创建模块作用域的变量。ES 模块还有隐式的 strict 模式,这可以防止全局变量泄漏。

🌐 This rule is mostly useful for browser scripts. Top-level declarations in ES modules and CommonJS modules create module-scoped variables. ES modules also have implicit strict mode, which prevents global variable leaks.

默认情况下,此规则不检查 constletclass 声明。

🌐 By default, this rule does not check const, let and class declarations.

规则详情

🌐 Rule Details

varfunction 声明

🌐 var and function declarations

在使用浏览器脚本时,开发者经常忘记顶层作用域的变量和函数声明会成为 window 对象上的全局变量。与具有自身作用域的模块不同。如果想要成为全局变量,应明确分配给 windowself。否则,打算作为脚本本地变量的变量应该封装在 IIFE 中。

🌐 When working with browser scripts, developers often forget that variable and function declarations at the top-level scope become global variables on the window object. As opposed to modules which have their own scope. Globals should be explicitly assigned to window or self if that is the intent. Otherwise variables intended to be local to the script should be wrapped in an IIFE.

此规则不允许在顶层脚本范围内声明 varfunction。由于 ES 和 CommonJS 模块具有模块作用域,因此此规则不适用于它们。

🌐 This rule disallows var and function declarations at the top-level script scope. This does not apply to ES and CommonJS modules since they have a module scope.

此规则的错误代码示例:

🌐 Examples of incorrect code for this rule:

在线运行
/*eslint no-implicit-globals: "error"*/

var foo = 1;

function bar() {}

符合此规则的正确代码示例:

🌐 Examples of correct code for this rule:

在线运行
/*eslint no-implicit-globals: "error"*/

// explicitly set on window
window.foo = 1;
window.bar = function() {};

// intended to be scope to this file
(function() {
  var foo = 1;

  function bar() {}
})();

此规则在 ESLint 配置中使用 "languageOptions": { "sourceType": "module" }正确代码示例:

🌐 Examples of correct code for this rule with "languageOptions": { "sourceType": "module" } in the ESLint configuration:

在线运行
/*eslint no-implicit-globals: "error"*/

// foo and bar are local to module
var foo = 1;
function bar() {}

全局变量泄漏

🌐 Global variable leaks

当代码不在 strict 模式时,对未声明变量的赋值会创建一个新的全局变量。即使代码在函数中,这种情况也会发生。

🌐 When the code is not in strict mode, an assignment to an undeclared variable creates a new global variable. This will happen even if the code is in a function.

这不适用于 ES 模块,因为模块代码隐式地处于 strict 模式。

🌐 This does not apply to ES modules since the module code is implicitly in strict mode.

此规则的错误代码示例:

🌐 Examples of incorrect code for this rule:

在线运行
/*eslint no-implicit-globals: "error"*/

foo = 1;

Bar.prototype.baz = function () {
    a = 1; // Intended to be this.a = 1;
};

只读全局变量

🌐 Read-only global variables

此规则还禁止重新声明只读全局变量和分配给只读全局变量。

🌐 This rule also disallows redeclarations of read-only global variables and assignments to read-only global variables.

只读全局变量可以是内置的 ES 全局变量(例如 Array),也可以是在配置文件中定义为 readonly 的全局变量,或者是在 /*global */ 注释中的全局变量。

🌐 A read-only global variable can be a built-in ES global (e.g. Array), or a global variable defined as readonly in the configuration file or in a /*global */ comment.

另请参阅:指定全局

🌐 See also: Specify Globals

此规则的错误代码示例:

🌐 Examples of incorrect code for this rule:

在线运行
/*eslint no-implicit-globals: "error"*/

/*global foo:readonly*/

foo = 1;

Array = [];
var Object;

exported

你可以像在 no-unused-vars 中一样使用 /* exported variableName */ 块注释。详情请参见 no-unused-vars 导出部分

🌐 You can use /* exported variableName */ block comments in the same way as in no-unused-vars. See the no-unused-vars exported section for details.

用于 /* exported variableName */ 操作的正确代码示例:

🌐 Examples of correct code for /* exported variableName */ operation:

在线运行
/* eslint no-implicit-globals: error */
/* exported global_var */

var global_var = 42;

选项

🌐 Options

此规则有一个带有一个选项的对象选项:

🌐 This rule has an object option with one option:

  • 如果你希望此规则也检查 constletclass 声明,请将 "lexicalBindings" 设置为 true

constletclass 声明

🌐 const, let and class declarations

词法声明 constlet 以及 class 声明,会创建块级作用域的变量。

🌐 Lexical declarations const and let, as well as class declarations, create variables that are block-scoped.

然而,当在浏览器脚本的顶层声明这些变量时,这些变量并不是“脚本作用域”的。它们实际上是在全局作用域中创建的,可能会与其他脚本中的 varconstlet 变量以及 functionclass 声明发生名称冲突。这不适用于 ES 和 CommonJS 模块。

🌐 However, when declared in the top-level of a browser script these variables are not ‘script-scoped’. They are actually created in the global scope and could produce name collisions with var, const and let variables and function and class declarations from other scripts. This does not apply to ES and CommonJS modules.

如果该变量是脚本的局部变量,请使用块或立即调用的函数表达式 (IIFE) 封装代码。

🌐 If the variable is intended to be local to the script, wrap the code with a block or with an immediately-invoked function expression (IIFE).

"lexicalBindings" 选项设置为 false(默认)时,此规则的 正确 代码示例:

🌐 Examples of correct code for this rule with "lexicalBindings" option set to false (default):

在线运行
/*eslint no-implicit-globals: ["error", {"lexicalBindings": false}]*/

const foo = 1;

let baz;

class Bar {}

"lexicalBindings" 选项设置为 true 时,此规则的 错误 代码示例:

🌐 Examples of incorrect code for this rule with "lexicalBindings" option set to true:

在线运行
/*eslint no-implicit-globals: ["error", {"lexicalBindings": true}]*/

const foo = 1;

let baz;

class Bar {}

"lexicalBindings" 选项设置为 true 时,此规则的正确代码示例:

🌐 Examples of correct code for this rule with "lexicalBindings" option set to true:

在线运行
/*eslint no-implicit-globals: ["error", {"lexicalBindings": true}]*/

{
    const foo = 1;
    let baz;
    class Bar {}
}

(function() {
    const foo = 1;
    let baz;
    class Bar {}
}());

如果你打算创建一个全局的 constlet 变量,或一个全局的 class 声明,以便在其他脚本中使用,请注意,与传统方法相比存在某些差异,传统方法是 var 声明和将值赋给全局 window 对象的属性:

🌐 If you intend to create a global const or let variable or a global class declaration, to be used from other scripts, be aware that there are certain differences when compared to the traditional methods, which are var declarations and assigning to a property of the global window object:

  • 词法声明的变量不能有条件地创建。脚本不能先检查变量是否存在然后再创建一个新的。var 变量也总是会被创建,但重新声明不会导致运行时异常。
  • 词法声明的变量不会在全局对象上创建属性,这是消费脚本可能期望的。
  • 词法声明的变量正在遮蔽全局对象的属性,如果使用的脚本同时使用该变量和属性,可能会产生错误。
  • 词法声明的变量如果初始化抛出异常,可能会产生永久性的暂时性死区(TDZ)。即使是 typeof 检查也不能避免 TDZ 引用异常。

"lexicalBindings" 选项设置为 true 时,此规则的 错误 代码示例:

🌐 Examples of incorrect code for this rule with "lexicalBindings" option set to true:

在线运行
/*eslint no-implicit-globals: ["error", {"lexicalBindings": true}]*/

const MyGlobalFunction = (function() {
    const a = 1;
    let b = 2;
    return function() {
        return a + b;
    }
}());

"lexicalBindings" 选项设置为 true 时,此规则的正确代码示例:

🌐 Examples of correct code for this rule with "lexicalBindings" option set to true:

在线运行
/*eslint no-implicit-globals: ["error", {"lexicalBindings": true}]*/

window.MyGlobalFunction = (function() {
    const a = 1;
    let b = 2;
    return function() {
        return a + b;
    }
}());

何时不使用

🌐 When Not To Use It

在浏览器脚本的情况下,如果你希望能够在全局作用域中显式声明变量和函数,并且你的代码处于严格模式下,或者你不希望此规则对未声明的变量发出警告,并且你也不希望此规则对只读全局变量发出警告,你可以禁用此规则。

🌐 In the case of a browser script, if you want to be able to explicitly declare variables and functions in the global scope, and your code is in strict mode or you don’t want this rule to warn you about undeclared variables, and you also don’t want this rule to warn you about read-only globals, you can disable this rule.

在 CommonJS 模块的情况下,如果你的代码处于严格模式,或者你不希望此规则对未声明的变量发出警告,并且你也不希望此规则对只读全局变量发出警告,你可以禁用此规则。

🌐 In the case of a CommonJS module, if your code is in strict mode or you don’t want this rule to warn you about undeclared variables, and you also don’t want this rule to warn you about the read-only globals, you can disable this rule.

对于 ES 模块,如果你不希望此规则警告你有关只读全局变量的信息,你可以禁用此规则。

🌐 In the case of an ES module, if you don’t want this rule to warn you about the read-only globals you can disable this rule.

版本

此规则是在 ESLint v2.0.0-alpha-1 中引入。

进阶读物

资源