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
Add a ment  | 

2 Answers 2

Reset to default 7

You 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