我正在努力使“使用严格”;指令起作用,并且有一些麻烦。在以下文件中,FireFox 9将(正确)检测到未在第3行上声明someVar,但无法检测到在第19行上未声明theVar。
"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!");
});
});
最佳答案
您需要在引发错误之前调用处理程序。换句话说,单击#btnToClick
。
fiddle 示例:http://jsfiddle.net/X3TQb/
关于javascript - 为什么 "use strict"(JavaScript)无法检测到未声明的变量?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8630541/