通过阅读文档,该设置undef似乎是控制“x 未定义”警告的原因。但将其设置为 false 并不会阻止这些警告。所以这让我想知道undef到底是什么。
有人可以比文档更好地解释这一点吗?
注意:要忽略这些警告,我必须使用/*jshint -W117 */
启用该undef选项后,只要发现非本地(范围内既不是参数也不是“var”变量)变量使用,就会发出警告。
以示例中的代码为例,结果如下'myvar' is not defined.(此警告表明代码运行时可能会导致引用错误;而不是该值是“未定义”。)
/*jshint undef:true */
function test() {
var myVar = 'Hello, World';
console.log(myvar); // Oops, typoed here. JSHint with undef will complain
}
Run Code Online (Sandbox Code Playgroud)
禁用该选项时,不会出现警告,因为假定意图是访问myvar gobal变量;反过来,这可以通过global 指令接受/验证,并且以下内容再次无警告。
/*jshint undef:true */
/*global myvar*/
function test() {
var myVar = 'Hello, World';
console.log(myvar); // Yup, we wanted the global anyway!
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
595 次 |
| 最近记录: |