javascriptscopeecma262strictecmascript-5

In ECMAScript5, what's the scope of "use strict"?


What scope does the strict mode pragma have in ECMAScript5?

"use strict";

I'd like to do this (mainly because JSLint doesn't complain about it):

"use strict";

(function () {
  // my stuff here...
}());

But I'm not sure if that would break other code or not. I know that I can do this, which will scope the pragma to the function...

(function () {

  "use strict";

  // my stuff here...

}());

but JSLint complains about it (when the "strict" JSLint option is enabled) because it thinks you're executing code before you enable "use strict".

Here's my question. If I have fileA.js:

"use strict";
// do some stuff

and fileB.js:

eval( somecodesnippet ); // disallowed by "use strict"

and then include them in my html page in that same order, will the pragma be scoped to the file, or will the pragma bleed over into fileB, thus blocking the eval execution?


Solution

  • EDIT It appears I was incorrect. Please see Jeff Walden's answer below.

    Check out this answer to a related question: What does "use strict" do in JavaScript, and what is the reasoning behind it?

    Despite JSLint's complaints, you can (and should) use "use strict"; inside of a function if you only want that function to be in strict mode. If you use it in the global context then it will force all your code to be in strict mode. Short answer: yes, it will block your use of eval.