Here is my code that seems to indicate that the answer is yes - http://jsfiddle.net/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'
?
Strict mode prohibits function statements that are not at the top level of a script or function. In normal mode in browsers, function statements are permitted "everywhere".
First, strict mode eliminates some JavaScript silent errors by changing them to throw errors. Second, strict mode fixes mistakes that make it difficult for JavaScript engines to perform optimizations: strict mode code can sometimes be made to run faster than identical code that's not strict mode.
Strict Mode in Function For example, myVariable = 9; console. log(myVariable); // 9 function hello() { // applicable only for this function 'use strict'; string = 'hello'; // throws an error } hello(); If you use 'use strict'; inside a function, the code inside the function will be in strict mode.
what are the advantages and disadvantages to using it? If you put "use strict"; at the top of your code (or function), then the JS is evaluated in strict mode. Strict mode throws more errors and disables some features in an effort to make your code more robust, readable, and accurate.
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
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.org/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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With