问题
Should I 'use strict' for every single javascript function I write?
What is a good practice to use strict in a large AngularJS project? Using it globally can break a third party library that doesn't support it, but doing 'use strict' every single time is just a lot of repetition.
回答1:
On this question, do beware a general tendency to oversimplify.
First, all of your code absolutely should be run in strict mode. Core modern javascript functionality is changed (see .call() and apply()) or disfigured (silent Errors) by executing code outside of strict mode. (More on this here, from Crockford.)
However, that doesn't address how you should ensure that your code runs in strict mode. There are at least two contexts to consider:
In the browser, your code should be delivered after being minified. If you include 'use strict'
in every function body, your minifier won't strip it out, and you'll waste bytes repeating it everywhere. You only need it in your outermost function scope(s)—at the top of your module definitions. One approach is to wrap your minified code in a single IIFE closure as part of the build process:
;(function (){
'use strict'
// All code here.
}())
This, I think, is close to the ideal way of doing it, but it more or less dictates that you adopt a continuous integration workflow (since, to observe your code running in strict mode, you must have it all wrapped up one closure). If you don't work that way, you'll have to include the use strict
directive at the top of every function that isn't being declared within another function's scope.
On the server, it's much simpler, of course. Code isn't minified, and bytes don't matter, so you can just include the use strict
directive at the top of every file.
A word of warning on --use_strict
: In cases where you control how scripts are run, you may find yourself tempted to use the --use_strict
runtime flag. It's easy, but it means all dependencies must be strict mode compliant. Since you can't control every third-party's compliance, this is usually unwise.
回答2:
All code you write1 should be in strict mode. It helps you catch mistakes by not ignoring exceptions.
However, no, this does not mean that you need to prepend "use strict";
to every function
definition, you only should put it in the module scope - once per file - so that it is inherited by all your functions. When you're going to switch to ES6 modules it will be implied anyway.
1: I'd even argue for enabling it globally, as strict mode should not break any properly written code.
If it does break a third party script, the solution might not be to disable strict mode again…
回答3:
Short answer, yes! You don't need to include it for every function, but rather you can just add it once per JavaScript file. When you start the file, start with a closure like this:
(function () {
"use strict";
// Rest of your code.
})();
回答4:
No. The redundancy is not necessary. Use of the declaration
"use strict";
at the file or stream scope is preferable, especially if third party libraries are used. It helps decide whether to use them as is, wrap them, or pass on using them.
File Level Self-executing Functions
It is becoming more common to see entire files or streams in self-executing closures. In such cases, the declaration to use strict mode (above) is inserted in the first line of the closure, where the ... is.
(function () {
...
}())
It may be useful to mention that self-execution is not always necessary and can actually cause sluggish loads and other problems if overused.
More on Scope of Declaration
The scope of is dependent on whether you place the declaration inside or outside a function and applies to all statements following the declaration within the closure's scope.
Use of the declaration inside the function is the wrong way to do it if all of the file or stream is already compatible with the stricter mode or can be made so with ease. The redundancy is unnecessary, so placing the declaration on the top of the file or beginning of the stream is the recommended use.
Sometimes only some of the functions comply with the stricter rules. In such cases the less desirable function scope of the declaration can be used to encourage finer coding practices at least in the functions that already comply.
Why Strict Mode At All?
Strict mode eliminates certain permissive language parsing and execution characteristics deemed less desirable from a language design point of view. These non-strict mode language characteristics were deemed prudent as default behavior for backward compatibility. The synopsis and details appear here.
- ECMA-262 7.0 Strict Mode Code
- ECMA-262 7.0 Strict Mode in detail
It is not unambiguously defined when and if these older language characteristics from the early Netscape days will be deprecated or whether strict mode will become default behavior across browsers at some point, but the stricter model is likely to produce less ambiguous and risky source. If you wish to improving maintainability, portability, and extensibility in coding practice and code bases, then strict mode is a good option.
NOTE For those coming here from "What's the benefit of using "function() 'use strict'” in every file?"
You can place
"use strict";
on the top of the code sequence or inside the function, so it is only one line of code per file or per function.
The other lines of code have other purposes in the code here.
- Self-invoking function
- Factory invocation
Some place the entire file in a self-executing function, but that is not necessary in every case.
来源:https://stackoverflow.com/questions/34301881/should-i-use-strict-for-every-single-javascript-function-i-write