javascript 禁用“使用 use strict 的函数形式”但保留“缺少‘use strict’语句”警告
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8953654/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Disable "use the function form of use strict" but keep the "Missing 'use strict' statement" warning
提问by Randall Flagg
I am using jslint to validate my code.
I have "use strict" on all my pages.
How can I disable the message "use the function form of 'use strict'" but keep the "Missing 'use strict' statement" warning, so I won't forget to put it on new files?
我正在使用 jslint 来验证我的代码。
我的所有页面上都有“严格使用”。
如何禁用消息“使用'use strict'的函数形式”但保留“缺少'use strict'语句”警告,以便我不会忘记将其放在新文件中?
Thanks
谢谢
回答by Paul Armstrong
According to Crockford's post, you'll want to wrap everything in a function...
根据Crockford 的帖子,您需要将所有内容都包装在一个函数中......
(function () {
"use strict";
// the rest of your file goes here...
}());
You could also use jshintinstead, which has a "globalstrict" option that can do exactly what you're asking for without having to wrap everything in a function
您也可以改用jshint,它有一个“ globalstrict”选项,可以完全满足您的要求,而无需将所有内容都包装在一个函数中
回答by Glenn Roberts
Cannot be done without changing the javascript file which drives jslint.
如果不更改驱动 jslint 的 javascript 文件,就无法完成。
To me function form is a cranky working practice, therefore cannot force on others.
对我来说,函数形式是一种胡思乱想的工作实践,因此不能强加于人。
Not everybody needs to combine and minify, but even if I did I'd combine code that applied the same rules, thus a file statement would be sufficient.
不是每个人都需要组合和缩小,但即使我这样做了,我也会组合应用相同规则的代码,因此文件语句就足够了。
Although jshint has exactly the feature you require. The latest jslint is now more advanced than jshint, spotting more weaknesses and copes with more complicated code. I like jshint but it isn't keeping up with jslint.
尽管 jshint 具有您需要的功能。最新的 jslint 现在比 jshint 更先进,发现更多弱点并处理更复杂的代码。我喜欢 jshint,但它跟不上 jslint。
回答by The Trav
The solution I found for this was to create a single line file with "use strict"; and nothing else
我为此找到的解决方案是创建一个带有“use strict”的单行文件;没有别的
Make that the first file in your concatenation package, add it to jslint's exclude list, switch the sloppy=true pragma
将其设为连接包中的第一个文件,将其添加到 jslint 的排除列表中,切换 sloppy=true 编译指示
There may be some side effects around not picking up sloppy code, but my understanding of the docs is that it just checks for the "use strict"; line
不选择草率代码可能会有一些副作用,但我对文档的理解是它只是检查“使用严格”;线
回答by user847319
Here is a hack to suppress "Use the function form of 'use strict'."
这是一个 hack 来抑制“使用‘use strict’的函数形式。”
$ uname -a
Darwin 13.0.0 Darwin Kernel Version 13.0.0
Figure out where your jslint distribution is.
$ which jslint /usr/local/bin/jslint $ ls -l /usr/local/bin/jslint lrwxr-xr-x 1 root admin 40 11 Feb 2013 /usr/local/bin/jslint -> ../lib/node_modules/jslint/bin/jslint.js $ cd /usr/local/lib/node_modules/jslint/ $ ls LICENSE README.md lib package.json Makefile bin node_modules
Comment out the warning.
$ sudo vim lib/jslint.js search for 'function_strict' comment out the line 'warn('function_strict');' note: the exact line might vary on some versions but just comment it out.
If it does not work you probably have multiple versions of jslint installed and have not edited the right one.
sudo find / -name jslint.js
找出您的 jslint 发行版在哪里。
$ which jslint /usr/local/bin/jslint $ ls -l /usr/local/bin/jslint lrwxr-xr-x 1 root admin 40 11 Feb 2013 /usr/local/bin/jslint -> ../lib/node_modules/jslint/bin/jslint.js $ cd /usr/local/lib/node_modules/jslint/ $ ls LICENSE README.md lib package.json Makefile bin node_modules
注释掉警告。
$ sudo vim lib/jslint.js search for 'function_strict' comment out the line 'warn('function_strict');' note: the exact line might vary on some versions but just comment it out.
如果它不起作用,您可能安装了多个版本的 jslint 并且没有编辑正确的版本。
sudo find / -name jslint.js