最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Can't inspect scope variable when using strict mode - Stack Overflow

programmeradmin3浏览0评论

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.

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.

Share Improve this question edited Dec 17, 2011 at 16:45 Timm asked Dec 17, 2011 at 13:51 TimmTimm 2,6623 gold badges26 silver badges35 bronze badges 9
  • 2 I think the error must be elsewhere; it doesn't happen to me using this test page in Safari (I can't set the breakpoint, but I don't get any error and I do get the expected output). Can you post a complete replicating test case to the question (and ideally also to jsbin.com or jsfiddle.net). – T.J. Crowder Commented Dec 17, 2011 at 14:01
  • I can't put my finger on it, but something tells me that you have a with statement somewhere in your code... I'm totally guessing here, of course... (Btw with statements throw in strict mode code.) – Šime Vidas Commented Dec 17, 2011 at 14:07
  • do you have a 'with()' in your code somewhere else? with() isn't liked and many want it discontinued in ECMAScript – Mike Sav Commented Dec 17, 2011 at 14:07
  • @MikeSav The decision has already been made. The with statement has been removed from the strict mode, and we don't expect it in ES6. – Šime Vidas Commented Dec 17, 2011 at 14:11
  • 1 @T.J.Crowder The above snippet is the whole code. I ran your testcase and with the breakpoint set, I get the same error (expectedly). The console.log output works fine in both cases, that has never been an issue for me. (It's just there to be able to set a breakpoint in the inspector.) – Timm Commented Dec 17, 2011 at 14:33
 |  Show 4 more comments

1 Answer 1

Reset to default 24

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.

发布评论

评论列表(0)

  1. 暂无评论