no-undef
除非在 /*global */
注释中提及,否则不允许使用未声明的变量
配置文件 中的
"extends": "eslint:recommended"
属性启用了该规则
此规则可以帮助您定位因变量和参数名称拼写错误或意外隐式全局变量(例如,忘记 for
循环初始化程序中的 var
关键字)导致的潜在 ReferenceErrors。
规则详情
对未声明变量的任何引用都会导致警告,除非该变量在 /*global ...*/
注释中明确提及,或在 配置文件中的 globals
键 中指定。这些的一个常见用例是,如果您有意使用在其他地方定义的全局变量(例如,在源自 HTML 的脚本中)。
此规则的错误代码示例:
/*eslint no-undef: "error"*/
var foo = someFunction();
var bar = a + 1;
w6SJeK7ZX7dUC6CB/3zOl/MBK47XJG8DRjvzc2PnDwkaQtkvto528E9qvWWyC2RLnZcpRYTgWUJ+naQsP9p7ChBPI7PCbi0/I4jYavai/+U=
/*global someFunction, a*/
/*eslint no-undef: "error"*/
var foo = someFunction();
var bar = a + 1;
M1ZZCXemGAXlq73jcQ7JHEZhS6N9wIHaGgQdTDOEIJEOWuuLtZahY1vv1m9YBDTE+wZwjz8s9B/qwt6EY4pmkVOramR5T710CED4eia0xLssAEcBVDa7upJRVvxpR+n1ji2pXB7x0CAZm7FnBleILCGdHdyv3Xtxsjt9nkC9Vz5RNkBUlyMWAB+uZe0YRpN3EeN1TyZ4ercXBcaemUvdFExC/E6rz3JtK7Mq78o6yFw=
zyUNp6n4acwrZTwzSGRQfYDy9WK3kHrYu2mrpFKHaewQLgi+u//KPf0fgx9xKZmCCFhR+uGGd9NoVdSHmvAYwJfivoqj28Hml1ORqL19UfM2QTYJw51MBR7RIyMySmU0qx7IwE5Cs/s8wdefT1ikL2MLFWSxgG1+LHhXOwVl7UWFP5w/KYlg46ZCeIPdGNNeBr/xzl2xm2MzQzW1/L+cMmrxsN4+09BevpLDFlrvrjM=
选项
- 2+78xlyhL2ODiiw5yVeIeyFIsN2aXC/kCp5hL9JMHAj7tUd2z1dCwisd7PjKSaorDARNWU+yhAYErLGux/fPJ7rC/KykV//AYkSqizuDtlKF9fvmwmGc/Lie1lg1TyCTZ2LTrtQhaqRqCa7L0S9YoCrwYa8HFpGyEANEtGjceqk=
typeof
CK7rMP54eg3IVkC5as6csnn1xPaFpqDzsquTi9vGjuEusHNcacxWDU+SFAanZuCb6kHLPFlkcbNfNw2BuWd/rWusMqZqQCRUcwcdaJA8yJU=
/*eslint no-undef: "error"*/
if (typeof UndefinedIdentifier === "undefined") {
// do something ...
}
3uk+J9rbMwgwt7ueZzdR5rm6m3rF2R7xifERzNOpJPn3PoTxcE8UWH1rYQ6kpr1zoNpIsF/1Fv/dxJt9hPBhnvtRgWpjBP1SBkA0Svo1ESylmLCJV0MEkaIYPy9vVsrwSgsLFLcCj2X0FBg9x3R9pQ==
vMwj86Gl/owxML5GU8RXZt26/beE7HPIGkWZv5mpm4CRC2vidL//9MVw+o4KSFGUXduPFJAdtIrYA4oZTK75mQ==
/*eslint no-undef: ["error", { "typeof": true }] */
if(typeof a === "string"){}
w6SJeK7ZX7dUC6CB/3zOl/MBK47XJG8DRjvzc2PnDwk5pkwQ4ORyhoXzKjFX63541mBk53eDcIX/zNovokoCWTTiEW1WAbFB69OIko9WQaQSjT18tsQE72qdkcVKC7FxvHEgS/YVZ2cPBkGNV9zeXg==
/*global a*/
/*eslint no-undef: ["error", { "typeof": true }] */
if(typeof a === "string"){}
环境
ltOhpHjW5uzRTSCTvsoeKnYkQHE9W+RVvosHk8eZSxgG78ZnjLOj+T9oh+dvAmw7DdU/Znas0LtUDM2NHeGbO7m79SxP/Hgkf+rtQivW/xw1c8wLnkFAh9JBqdILzpT2nS3BqfduWCLqZBEUoinNQtwhaarDha6OFVTrXH2szDsIG8cnYr7XBgtgOnRdlEh7YdkNbzXDfwhdI6p9iP+90yzYFhrtVF5SJOdyf6vx8jlYaQjbXM49VG0Osha51xE7nJCsnJY5rgUTHcWLX4lZYNd+EGfKLB1BkJ8MeU27dX3K6NP+hk+Mqy92de7zp0iuvoTN8ea6WWrDUkt+/y5yfw==
browser
S+LvFWOSzCKGfJ18M6555rQ1BpKoCNZcOO0OaF81ZUVd/0LUVkoZvGzFKqj0vIheq/STFS3HZ7q8k8gaMbLHxg==
/*eslint no-undef: "error"*/
/*eslint-env browser*/
setTimeout(function() {
alert("Hello");
});
Node.js
xZh0B9cM91LMnbgHTCLc3XC09dIUlZocfrq09jj5HiuMFQpEiPSV8aSmBRgXdhX0KPNzPxOg3Am28DZYd5rJkA==
/*eslint no-undef: "error"*/
/*eslint-env node*/
var fs = require("fs");
module.exports = function() {
console.log(fs);
};
何时不使用
cAyp3IO/K/1v6n7IfqmRFkOYVYVRRHYV06GiLmiwBT7uaz5yMcICVJQloZfzUcSx6AyTOSiDBKuu+096jYYWjQ==
兼容性
+l+3HPAYcPBG7Zy3m3NFaRDkst8xsh6vlcS2CimLL9V93r6sluBoQKvwwMP7Lp3dF8XuvChwr64JHYUprvYZgP6lJykPccTBaSmhvhmjMn0zYBRBqbGWcm3o/cj/B3blBy/NgVC8+J8jtAqQOJKEbA7rhcv0yvWmk1GdC+Q2SeaAyaeNv615CaTowceeHNjnpmG9yZKHZeGSE2uq8YLqx/7iEsxZjnzFPaOiYQRm8Ew=