问题
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.
回答1:
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.
回答2:
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.
来源:https://stackoverflow.com/questions/4769477/how-to-enable-ecmascript-use-strict-globally