Disallow specific global variables (no-restricted-globals)

禁用特定的全局变量 (no-restricted-globals)

Disallowing usage of specific global variables can be useful if you want to allow a set of global variables by enabling an environment, but still want to disallow some of those.


For instance, early Internet Explorer versions exposed the current DOM event as a global variable event, but using this variable has been considered as a bad practice for a long time. Restricting this will make sure this variable isn’t used in browser code.

例如,早期的 IE 版本暴露了当前 DOM 事件 event 作为一个全局变量,但是使用这个变量长时间内被认为不是一个好的实践。限制它的使用需要确保这个变量没有被用在浏览器端的代码中。

Rule Details

This rule allows you to specify global variable names that you don’t want to use in your application.



This rule takes a list of strings which are the global variable names.


Examples of incorrect code for sample "event", "fdescribe" global variable names:

全局变量 "event", "fdescribe"错误 代码示例:

/*global event, fdescribe*/
/*eslint no-restricted-globals: ["error", "event", "fdescribe"]*/

function onClick() {

fdescribe("foo", function() {

Examples of correct code for a sample "event" global variable name:

全局变量 "event"正确 代码示例:

/*global event*/
/*eslint no-restricted-globals: ["error", "event"]*/

import event from "event-module";
/*global event*/
/*eslint no-restricted-globals: ["error", "event"]*/

var event = 1;


This rule was introduced in ESLint 2.3.0.

该规则在 ESLint 2.3.0 中被引入。