So, I'm going to answer my own question here (since I've figured it out).
My initial premise was that both code blocks are equivalent. So that this
var f = function ( eval ) {};
is equivalent to
var f = new Function( 'eval', '' );
This is however not true. There are differences. The creation of a function object from a function declaration / expression notation is defined in Chapter 13.2 Creating Function Objects. On the other hand, the creation of a function object from a new Function
constructor invocation is defined in Chapter 15.3.2.1 new Function (p1, p2, … , pn, body). So there are different algorithms at work here.
The specific part which is relevant to this question is the part which defines the strictness of the created function object.
Function expressions
The strictness of a function object created via a function expression is defined in the semantics of the production FunctionExpression at the beginning of chapter 13:
Pass in true as the Strict flag if the FunctionExpression is contained
in strict code or if its FunctionBody is strict code.
So the function object will be strict if either one of these conditions is met:
- the function expression is contained in strict code
- the function body of the function expression is strict code
so for instance, the function f
is strict in both examples below.
Example 1:
(function () {
var f = function () {
'use strict';
return 'I am strict!';
}
})();
Example 2:
(function () {
'use strict';
var f = function () {
return 'I am strict!';
}
})();
Function constructor invocation
The strictness of a function object created via a Function constructor invocation is defined in step 9 of the algorithm from Chapter 15.3.2.1 (already linked above):
If body is strict mode code (see 10.1.1) then let strict be true, else let strict be false.
So, whether or not the new Function
invocation is contained in strict code is irrelevant. To create a strict function via this pattern, one has to explicitly define the strictness in the function body (which is the last argument supplied to the constructor.
new Function ( 'a, b', 'return a + b;' ); // not strict
new Function ( 'a, b', '"use strict"; return a + b;' ); // strict