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.
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.
Share Improve this question asked Jan 22, 2011 at 17:48 TowerTower 103k131 gold badges364 silver badges521 bronze badges 3- 9 Placing "use strict" in every file seems like a scriptable task. – user395760 Commented Jan 22, 2011 at 17:51
- I guess I could try macros in my IDE for this one. Now I would have a reason to learn them... – Tower Commented Jan 22, 2011 at 17:54
- @delnan, Well it's better if it doesn't need to be done that way. – Pacerier Commented Apr 20, 2016 at 16:17
2 Answers
Reset to default 3Well, 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.
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 mand.
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 mand line.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745083867a4610287.html
评论列表(0条)