Reputation: 13023
I understand it shouldn't be done, take this as an academic question.
If I was to, say, concatenate several JavaScript files, some with 'use strict';
at the top, and some without, what would be the outcome?
I know that if you put the phrase at the top of the file it enables strict mode for the entire file, and if it is at the start of a function it is scoped to that function.
But what if the first 'use strict';
is mid-way down? I guess you can extrapolate this quest to function-scoped uses as well.
Will it;
E.g.
/*from file1.js*/
function bob() {
willThisError = 'because of use strict mode below?';
}
/*from file2.js*/
"use strict";
function bob2() {
var thisAuthor = 'likes to enable strict mode';
}
or
//midway through function
function test() {
willThisError = '?';
'use strict';
var orWillIt = 'have no effect';
}
I guess I could just test it myself, but I'm interested in a spec reference or something, and not to get caught up in browsers' individual interpretations on strict mode declaration.
Upvotes: 1
Views: 1605
Reputation:
The effect of concatenating strict mode
scripts with non-strict mode scripts depends on the order in which you do it. If you start with a strict
script, the whole script will look strict
. If you start with a non-strict one, it'll look non-strict.
You can find a good reference here
Upvotes: 1
Reputation: 187034
According to the MDN documentation
Strict mode applies to entire scripts or to individual functions. It doesn't apply to block statements enclosed in {} braces; attempting to apply it to such contexts does nothing. eval code, Function code, event handler attributes, strings passed to setTimeout, and the like are entire scripts, and invoking strict mode in them works as expected.
So I would take that to mean that at the top of the file or at the top of functions, or it does nothing.
Upvotes: 1