Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't inspect scope variable when using strict mode

When running the following script in Safari (with the Inspector open)

window.onload = function() { 
    "use strict";
    var x = 1;
    debugger; // debugger will auto-break on this line
}

I get the following error when asking for x in the console while being on that breakpoint:

Error
message: "'with' statements are not valid in strict mode"

Removing the "use strict"; line lets me access x from the console while on that breakpoint.

Both times the x is shown under Scope Variables in the sidebar.

like image 956
Timm Avatar asked Dec 17 '11 13:12

Timm


People also ask

Is it possible to use undeclared variables in strict mode?

With strict mode, you can not, for example, use undeclared variables. The numbers in the table specify the first browser version that fully supports the directive. You can use strict mode in all your programs. It helps you to write cleaner code, like preventing you from using undeclared variables.

When would you not use strict mode?

If you have such an unrestrictedly typed code, that is used variables without declaring. One variable declared within some function/scope and used from somewhere else(it will be undeclared there) and you can't rewrite/change them, then you should not go for "use strict;" mode because it will break the code.

How do I get rid of use strict?

No, you can't disable strict mode per function. Notice how we can define function outside of strict code and then pass it into the function that's strict. You can do something similar in your example — have an object with "sloppy" functions, then pass that object to that strict immediately invoked function.

Should I always use strict mode?

Turning on strict mode helps you find errors sooner, before they become bigger problems. And it forces you to write better code. Always use strict mode on your scripts.


1 Answers

This appears to be a known issue with Safari: https://bugs.webkit.org/show_bug.cgi?id=65829

To reproduce the Error, you simply need to type any code into the console while stopped at a breakpoint and while in strict mode.

Here's the code from the bug report:

(function(){
    "use strict";
    debugger;
})();

So when you're at the breakpoint, go to the console and type 2+3 (or any expression), and you'll get the Error.

enter image description here

like image 105
RightSaidFred Avatar answered Oct 09 '22 23:10

RightSaidFred