How does the strict mode ("use strict";) inherited by functions work?

Here is my code that seems to indicate the answer is yes - http://jsfiddle.net/4nKqu/

var Foo = function() { 'use strict' return { foo: function() { a = 10 alert('a = ' + a) } } }() try { Foo.foo() } catch (e) { alert(e) } 

Could you please quote from a standard explaining that 'use strict' automatically applies to all closures and functions defined in the function to which we apply 'use strict' ?

+7
javascript use-strict ecmascript-5 strict
source share
2 answers

Relevant part of the specification:

http://www.ecma-international.org/ecma-262/5.1/#sec-10.1.1

which reads:

 Code is interpreted as strict mode code in the following situations: 
  • Global code is strict global code if it starts with a directory prolog containing the Use Strict directive (see section 14.1).

  • Eval code is strict eval code if it starts with the Prologue directive, which contains the Use Strict Directive line, or if the eval call is a direct call (see 15.1.2.1.1) to the eval function, which is equal to those contained in strict mode.

  • Function code that is part of a FunctionDeclaration, FunctionExpression, or accessor PropertyAssignment function is a strict code function if its FunctionDeclaration, FunctionExpression, or PropertyAssignment is contained in strict mode code or if the function code begins with a directive prolog containing the Use Strict Directive.

  • The function code that is supplied as the last argument to the built-in function constructor is a strict function code if the last argument is a String, which, when processed as a FunctionBody, starts with the Prolog directive containing the line "Use a strict directive".

So, for functions defined explicitly within the "strict scope", they inherit strict mode:

 function doSomethingStrict(){ "use strict"; // in strict mode function innerStrict() { // also in strict mode } } 

But functions created using the Function constructor do not inherit strict mode from their context, so they must have an explicit "use strict"; statement "use strict"; if you want them to be in strict mode. For example, noting that eval is a reserved keyword in strict mode (but not outside of strict mode):

 "use strict"; var doSomething = new Function("var eval = 'hello'; console.log(eval);"); doSomething(); // this is ok since doSomething doesn't inherit strict mode 
+8
source share

The answer is yes, but you probably won't find the exact sentence in the documentation; instead, it discusses contexts. When you define a Foo function inside another Bar function, Foo is created in the context of Bar . If the Bar context is in strict mode, this means that the Foo context is in strict mode.

You can see the documentation here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions_and_function_scope/Strict_mode

If you think about it, the absence of such behavior will be truly impractical (and there is no real flaw).

It is also useful for your entire library to use strict mode without any problems when combining multiple scripts:

You can also use a way to wrap the entire contents of a script in a function, and the presence of this external function uses strict mode.

+2
source share

All Articles