How is strict mode (“use strict”;) inherited by functions?

丶灬走出姿态 提交于 2019-12-10 04:23:05

问题


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'?


回答1:


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



回答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.



来源:https://stackoverflow.com/questions/19135608/how-is-strict-mode-use-strict-inherited-by-functions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!