19

I'm getting this error in safari console when I try to do anything from a breakpoint. Bug report here: https://bugs.webkit.org/show_bug.cgi?id=83267

Does anyone have a workaround? For reasons that I am assuming are related to this javascript has ceased to run on my site in safari.

edit: the bug report also contains repro steps.

another edit: I'm not using a "with" statement. This is a bug from the safari console.

Damjan Pavlica
  • 31,277
  • 10
  • 71
  • 76
quinn
  • 5,508
  • 10
  • 34
  • 54
  • 1
    Not Safari-specific. `'use strict';with(this);` in FF/Chrome produces "strict mode code may not contain 'with' statements". – Rob W Aug 31 '12 at 16:11
  • 11
    It is specific to Safari in that if you try to evaluate anything in the debugger console within a `"use strict";` scope, you get this error, even if you can't see the word `with` anywhere in your script or in what you've typed. Apparently Safari wraps whatever you've typed in a `with` and tries to evaluate it. See the referenced bug report for more details. – Kristopher Johnson Jan 23 '13 at 01:01
  • 1
    possible duplicate of [Can't inspect scope variable when using strict mode](http://stackoverflow.com/questions/8545191/cant-inspect-scope-variable-when-using-strict-mode) – Lance Fisher Jan 13 '14 at 22:55

2 Answers2

9

The with(obj) {} statement is deprecated, and as such, is not valid in strict mode.

To solve this, either disable strict mode, or stop using with statements!

Eric
  • 95,302
  • 53
  • 242
  • 374
  • I'm technically not using with (safari is internally i guess?) but this is my doctype: ... is this what "strict" means in this context? – quinn Aug 31 '12 at 16:08
  • Nope. Strict mode is enabled with `"use strict"` at the top of your JS file. – Eric Aug 31 '12 at 16:09
  • 3
    So I guess the workaround would be to remove 'use strict' everywhere that I can find it until Safari fixes this bug? – quinn Aug 31 '12 at 16:15
  • @quinn: I guess so. Unless safari is using `"use strict"` internally as well. – Eric Aug 31 '12 at 16:16
  • 1
    @quinn: What if you add `"use strict";` inside a function, not within a file? Does that make any difference? That way it should only apply to the content of the function. – Tadeck Aug 31 '12 at 16:19
  • 5
    @Eric I removed 'use strict' and everything is working, it seems the safari debugger doesn't work within a context that has 'use strict'; :( thanks for your help! – quinn Aug 31 '12 at 16:34
  • 2
    It's not the OP using with, it's the Safari console code. – gotofritz Sep 30 '14 at 15:41
  • Why is it depricated? It lets you limit exposed scope nicely without inventing your own less expressive `function using(object, callback){ callback(object); }`. It's nice for `const obj = {}; using(require('fs'), (fs) =>{obj['one']=fs.readFileSync('one.txt'); obj['two']=fs.readFileSync('two.txt')})` – Dmytro Jul 08 '16 at 00:46
  • @Dmitry: Because it means name resolution and scoping rules have to happen at run time not compilation time. No other language construct in javascript behaves this way. – Eric Jul 08 '16 at 02:59
3

Strict mode in ECMAScript 5 bans eg. with statement. You have two choices:

  • disable strict mode (remove "strict mode"; line from the file / function), or
  • (preferred) avoid using with statements - they are harmful and totally unreadable.

More on strict mode from John Resig: ECMAScript 5 Strict Mode, JSON, and More.

You can read in the article, that strict mode:

  • makes you unable to delete variable (like in delete foo;),
  • limits usage of eval,
  • adds some limitations to functions (eg. accessing caller and callee),
  • removes with statements,
Tadeck
  • 132,510
  • 28
  • 152
  • 198