什么是JSHint选项" undef"做?

时间:2014-06-10 22:59:34

标签: javascript jshint

从阅读documentation设置undef似乎是控制" x未定义"警告。但是将其设置为false并不能阻止这些警告。所以这让我想知道undef实际上做了什么。

有人可以比文档更好地解释这个吗?

注意:要忽略这些警告,我必须使用/*jshint -W117 */

1 个答案:

答案 0 :(得分:4)

undef选项在启用时,只要找到非本地(既不是参数也不是" var"范围内的变量)变量用法,就会发出警告。

获取the example中的代码,以下结果为'myvar' is not defined.(此警告表示代码运行时代码可能导致ReferenceError;而不是值为"未定义& #34;。)

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

当禁用该选项时,没有任何警告,因为它假定意图是访问myvar gobal 变量;这可以通过global directive接受/验证,以下内容再次无警告。

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