繁体   English   中英

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

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

通过阅读文档 ,设置undef似乎是控制“ x未定义”警告的原因。 但是将其设置为false不会停止这些警告。 因此,这让我想知道undef实际上是做什么的。

有人可以解释比文档更好的解释吗?

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

启用undef选项后,只要发现使用非本地变量(范围中既不是参数也不是“ var”变量),就会发出警告。

示例中的代码为例 ,以下'myvar' is not defined.结果'myvar' is not defined. (此警告表明该代码在运行时可能会导致ReferenceError;而不是该值是“ undefined”。)

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

禁用该选项时,不会发出警告,因为它假定要访问myvar gobal变量。 反过来,可以使用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