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