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();
BYossarian
source share