Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SyntaxError: 'with' statements are not valid in strict mode

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.

like image 710
quinn Avatar asked Aug 31 '12 16:08

quinn


People also ask

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.

How do you declare strict mode?

Strict mode is declared by adding "use strict"; to the beginning of a script or a function.

Why this is undefined in strict mode?

This proves that this here refers to the global object, which is the window object in our case. Note that, if strict mode is enabled for any function then the value of this will be undefined because in strict mode global object refers to undefined in place of the window object.

What is correct way to run a JavaScript in strict mode?

Using Strict mode for a function: Likewise, to invoke strict mode for a function, put the exact statement “use strict”; (or 'use strict';) in the function's body before any other statements. Examples of using Strict mode: Example: In normal JavaScript, mistyping a variable name creates a new global variable.


2 Answers

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!

like image 101
Eric Avatar answered Oct 01 '22 12:10

Eric


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,
like image 21
Tadeck Avatar answered Oct 01 '22 13:10

Tadeck