admin管理员组文章数量:1355528
I'm trying to get the "use strict"; directive to work, and having a bit of trouble. In the following file FireFox 9 will (correctly) detect that someVar hasn't been declared on line 3, but fails to detect that theVar hasn't been declared on line 19. I'm stumped as to why this would be the case.
"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!");
});
});
I'm trying to get the "use strict"; directive to work, and having a bit of trouble. In the following file FireFox 9 will (correctly) detect that someVar hasn't been declared on line 3, but fails to detect that theVar hasn't been declared on line 19. I'm stumped as to why this would be the case.
"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!");
});
});
Share
Improve this question
edited Oct 30, 2012 at 4:02
hippietrail
17k21 gold badges109 silver badges179 bronze badges
asked Dec 25, 2011 at 16:56
MikeTheTallMikeTheTall
3,6134 gold badges35 silver badges50 bronze badges
0
2 Answers
Reset to default 7You need to invoke the handler before the error is thrown. In other words, click the #btnToClick
.
Example fiddle: http://jsfiddle/X3TQb/
Javascript is kinda funny when it es to variable scope. If you were to run different code before running this code, you could have the variables declared, and there wouldn't be any error, and for that reason it's hard to throw errors for missing variables except at runtime.
本文标签: jqueryWhy doesn39t quotuse strictquot (JavaScript) detect an undeclared variableStack Overflow
版权声明:本文标题:jquery - Why doesn't "use strict" (JavaScript) detect an undeclared variable? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743947896a2566747.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论