admin管理员组文章数量:1291117
To enable strict mode for all JavaScript, does the "use strict"
setting need to be at the top of every imported JavaScript file, at the top of the first file, or the top of any file?
There doesn't seem to be ay documentation on this aspect.
Thanks!
To enable strict mode for all JavaScript, does the "use strict"
setting need to be at the top of every imported JavaScript file, at the top of the first file, or the top of any file?
There doesn't seem to be ay documentation on this aspect.
Thanks!
Share Improve this question edited May 4, 2012 at 15:29 Donald T asked May 4, 2012 at 15:10 Donald TDonald T 10.7k17 gold badges65 silver badges93 bronze badges2 Answers
Reset to default 11It needs to be at the top of each script that you want strict
applied to.
But, if the scripts were concatenated through minification, then "use strict" at the top of the first file would apply to all files (as they'd then be in the same file).
Because of this perceived danger (3rd party libraries?), it's advised not to do this, and instead apply it inside an IIFE for each 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>
It goes at the top of each script, or if you only want it to apply to certain functions you can do something like:
// Non-strict code...
(function(){
"use strict";
// Your strict library goes here.
})();
// More non-strict code...
Here's a good article about it: http://ejohn/blog/ecmascript-5-strict-mode-json-and-more/
本文标签: Enabling strict mode for multiple JavaScript filesStack Overflow
版权声明:本文标题:Enabling strict mode for multiple JavaScript files - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741525544a2383444.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论