如何启用 ECMAScript “use strict”全球?
我有一个包含数百个 JavaScript 源文件的项目。我想知道为项目启用严格模式的最佳方法是什么?我了解此操作的后果,我只是寻求有关部署此功能的建议。在每个文件中放置“use strict”似乎并不有趣。
I have a project with literally hundreds of JavaScript source files. I am wondering what is the best way to enable the strict mode for the project? I understand the consequences of this action and I am only looking for advice regarding the deployement of this feature. Placing "use strict" in every file does not seem fun.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,我不清楚你的 javascript 文件将用于的上下文,但是假设上下文是一个动态 Web 应用程序,其中会在需要时加载各种页面文件、javascript 文件、样式表等,那么我会只需创建一个 javascript 文件,其中仅包含“use strict”。然后,将该文件包含在您的 head 标记中,位于所有其他 javascript 文件之前,并确保您是否要将 javascript 文件动态插入给定 Web 应用程序的文档头部,并将它们附加在“use strict”.js 之后文件。
这样您就不必检查每个 .js 文件并单独修改它们。
Well, I'm not clear on the context your javascript files will be used in, however say the context is a dynamic web application where various page files, javascript files, style sheets, etc, etc, are loaded when needed, then I would just create a single javascript file with only "use strict" within it. Then, include that file in your head tags, preceding all other javascript files and make sure that if you will be inserting javascript files dynamically into the head of the document of a given web application that you append them after your "use strict" .js file.
That way you won't have to go through each of your .js files and modify them individually.
正如我所见,不可能启用“use strict”;全球。我也遇到了类似的问题,我被迫编写脚本来轻松快速地完成它(我的项目中有超过 200 个文件),有些文件已经包含该语句,有些则没有,所以我在添加之前检查了它。在这里我附上我以 bash 命令形式编写的解决方案。
抱歉,这看起来很丑,但我没有在脚本文件中编写它,我只是在命令行中执行它。
As I see there is no possibility to enable "use strict"; globally. I had similar problem with it, and I were forced to write script to do it easily and quick (I had more that 200 files in project), some of files already contained that statement, some not, so I checked it before adding. Here I attach my solution written as bash command.
Sorry, that looks so ugly but I did not write this in script file I just execute it in the command line.