[英]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.