Skip to main content

使用全局变量

当你在 HTML 文件中包含定义全局变量的脚本并尝试在代码中使用这些变量之一时,linter 会报错,因为它看不到变量的定义。

¥When you include a script in the HTML file that defines global variables and try to use one of these variables in the code, the linter will complain because it cannot see the definition of the variable.

你可以通过从 window 对象中显式读取全局变量来避免这种情况,例如:

¥You can avoid this by reading the global variable explicitly from the window object, for example:

const $ = window.$;

这清楚地表明你是故意使用全局变量而不是因为拼写错误。

¥This makes it clear you are using a global variable intentionally rather than because of a typo.

或者,你可以通过在其后添加 // eslint-disable-line 来强制 linter 忽略任何行。

¥Alternatively, you can force the linter to ignore any line by adding // eslint-disable-line after it.