How is strict mode ("use strict";) inherited by functions?
Asked Answered
M

2

9

Here is my code that seems to indicate that 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 cite the statements from the standard that clarifies that 'use strict' is automatically applied to all closures and functions defined within a function to which we have applied 'use strict'?

Moralist answered 2/10, 2013 at 11:12 Comment(1)
You might want to consider explicitly declaring strict mode in every function anyway, otherwise it leaves room for error when the code is being refactored.Disfavor
D
8

The relevant part of the spec:

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

which says:

Code is interpreted as strict mode code in the following situations:
  • Global code is strict global code if it begins with a Directive Prologue that contains a Use Strict Directive (see 14.1).

  • Eval code is strict eval code if it begins with a Directive Prologue that contains a Use Strict Directive or if the call to eval is a direct call (see 15.1.2.1.1) to the eval function that is contained in strict mode code.

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

  • Function code that is supplied as the last argument to the built-in Function constructor is strict function code if the last argument is a String that when processed as a FunctionBody begins with a Directive Prologue that contains a Use Strict Directive.

So for functions defined explicitly within a 'strict scope', they will inherit strict mode:

function doSomethingStrict(){
    "use strict";

    // in strict mode

    function innerStrict() {
        // also in strict mode
    }
}

But functions created using the Function constructor don't inherit strict mode from their context, so must have an explicit "use strict"; statement if you want them 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
Dusty answered 2/10, 2013 at 11:16 Comment(0)
O
2

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

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

If you think about it, not having that behavior would be really impractical (and there is no real downside to it).

This is also helpful to make your entire library use strict mode without any issue in case of concatenation of several scripts:

You can also take the approach of wrapping the entire contents of a script in a function and having that outer function use strict mode.

Overliberal answered 2/10, 2013 at 11:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.