如何启用 ECMAScript “use strict”全球?

发布于 2024-10-14 01:55:09 字数 110 浏览 2 评论 0原文

我有一个包含数百个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

以可爱出名 2024-10-21 01:55:09

好吧,我不清楚你的 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.

不语却知心 2024-10-21 01:55:09

正如我所见,不可能启用“use strict”;全球。我也遇到了类似的问题,我被迫编写脚本来轻松快速地完成它(我的项目中有超过 200 个文件),有些文件已经包含该语句,有些则没有,所以我在添加之前检查了它。在这里我附上我以 bash 命令形式编写的解决方案。

对于path1 parh2 anotherPatr中的路径;对 $ 中的文件执行操作(find $path -name "*.js"); do if [[ $(grep "use strict" $file | wc -l) -gt 0 ]];然后 echo $file "已经使用严格"; else sed -i '1i\"使用严格\";' $文件;菲;完毕;完成;

抱歉,这看起来很丑,但我没有在脚本文件中编写它,我只是在命令行中执行它。

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.

for path in path1 parh2 anotherPatr; do for file in $(find $path -name "*.js"); do if [[ $(grep "use strict" $file | wc -l) -gt 0 ]]; then echo $file "already use strict"; else sed -i '1i\"use strict\";' $file; fi; done; done;

Sorry, that looks so ugly but I did not write this in script file I just execute it in the command line.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文