简体   繁体   English

JSHint选项“ undef”有什么作用?

[英]What does the JSHint option “undef” do?

From reading the documentation the setting undef seems to be what controls the "x is not defined" warnings. 通过阅读文档 ,设置undef似乎是控制“ x未定义”警告的原因。 But setting it to false does not stop those warnings. 但是将其设置为false不会停止这些警告。 So this leaves me wondering what undef actually does. 因此,这让我想知道undef实际上是做什么的。

Could someone please explain this better than the documentation does? 有人可以解释比文档更好的解释吗?

Note: To ignore these warnings I had to use /*jshint -W117 */ 注意:要忽略这些警告,我必须使用/*jshint -W117 */

The undef option, when enabled, warns whenever non-local (neither a parameter nor "var" variable in scope) variable usage is found. 启用undef选项后,只要发现使用非本地变量(范围中既不是参数也不是“ var”变量),就会发出警告。

Taking the code in the example , the following results in 'myvar' is not defined. 示例中的代码为例 ,以下'myvar' is not defined.结果'myvar' is not defined. (This warning indicates that the code may result in a ReferenceError when the code is run; not that the value is "undefined".) (此警告表明该代码在运行时可能会导致ReferenceError;而不是该值是“ undefined”。)

/*jshint undef:true */
function test() {
  var myVar = 'Hello, World';
  console.log(myvar); // Oops, typoed here. JSHint with undef will complain
}

When disabling the option there is no warning as it's assumed that the intent was to access the myvar gobal variable; 禁用该选项时,不会发出警告,因为它假定要访问myvar gobal变量。 this can in turn be accepted/validated with the global directive , and the following is once again warning-free. 反过来,可以使用global指令接受/验证它,并且以下内容再次没有警告。

/*jshint undef:true */
/*global myvar*/
function test() {
  var myVar = 'Hello, World';
  console.log(myvar); // Yup, we wanted the global anyway!
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM