最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How is strict mode ("use strict";) inherited by functions? - Stack Overflow

programmeradmin0浏览0评论

Here is my code that seems to indicate that the answer is yes - /

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

Here is my code that seems to indicate that the answer is yes - http://jsfiddle/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'?

Share Improve this question edited Nov 3, 2013 at 14:46 Ben Jackson 11.9k7 gold badges34 silver badges42 bronze badges asked Oct 2, 2013 at 11:12 Lone LearnerLone Learner 20.7k25 gold badges114 silver badges220 bronze badges 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. – Ingo Bürk Commented Nov 3, 2013 at 14:54
Add a ment  | 

2 Answers 2

Reset to default 8

The relevant part of the spec:

http://www.ecma-international/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

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/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.

发布评论

评论列表(0)

  1. 暂无评论