我試圖得到「嚴格使用」;指示工作,並有一些麻煩。在以下文件中,FireFox 9將(正確)檢測到第3行中沒有聲明someVar,但未能檢測到第19行中沒有聲明該變量。我很難理解爲什麼會出現這種情況。爲什麼不使用「嚴格」(JavaScript)檢測未聲明的變量?
"use strict"; // this will cause the browser to check for errors more aggresively
someVar = 10; // this DOES get caught // LINE 3
// debugger; // this will cause FireBug to open at the bottom of the page/window
// it will also cause the debugger to stop at this line
// Yep, using jQuery & anonymous functions
$(document).ready(function(){
alert("document is done loading, but not (necessarily) the images!");
$("#btnToClick").click(function() {
alert("About to stop");
var aVariable = 1;
debugger; // stop here!
alert("post stop " + aVariable);
// this lacks a "var" declaration:
theVar = 10; // LINE 19 // this is NOT getting caught
// needs a closing "
// alert("hi);
console.log("Program is printing information to help the developer debug a problem!");
});
});
作爲一個旁註,使用能夠通過linter分析代碼的編輯器將捕獲這些錯誤編輯時間。就我個人而言,我使用Sublime Text 2與SublimeLinter結合強調JSHint報告的錯誤http://www.jshint.com/ – 2011-12-25 17:09:00
堅果!我發誓我實際上已經嘗試了幾次,並沒有從Firebug得到任何錯誤。 我回去了&再試一次,現在我做的錯誤是在Firebug控制檯中報告(但只有當點擊)。 JSLint在一次通過中報告它(即,不等待該方法被調用)。 謝謝! – MikeTheTall 2011-12-26 05:23:58
您可能會指出答案中解析時間和運行時錯誤報告之間的差異。這是在這裏玩的關鍵概念。 – wewals 2012-10-30 04:19:00