Enabling strict mode for multiple JavaScript files

To enable strict mode for all JavaScript, "use strict"should the parameter be at the top of each imported JavaScript file, at the top of the first file, or at the top of any file?

In this aspect, there is no documentation on this issue.

Thank!

+2
source share
2 answers

It should be at the top of each script that you want to apply to strict.

But , if the scripts were combined using a mini-code, then "use strict" at the top of the first file will be applied to all files (since they will be in one file).

- ( ?), , IIFE script.

<script src="foo.js">
    (function () {
        "use strict";

        // Your code, don't forget you've now got to make things global via `window.blah = blah`
    }());
</script>
+6

script, , , - :

// Non-strict code...

(function(){
  "use strict";

  // Your strict library goes here.
})();

// More non-strict code... 

: http://ejohn.org/blog/ecmascript-5-strict-mode-json-and-more/

+2

Source: https://habr.com/ru/post/1667483/


All Articles