Should I 'use strict' for every single javascript function I write?

后端 未结 4 1097
逝去的感伤
逝去的感伤 2020-12-17 08:37

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

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-17 09:04

    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.

提交回复
热议问题