Javascript JSLint 突然报错:使用“use strict”的函数形式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4462478/
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
JSLint is suddenly reporting: Use the function form of "use strict"
提问by Zhami
I include the statement:
我包括以下声明:
"use strict";
at the beginning of most of my Javascript files.
在我的大部分 Javascript 文件的开头。
JSLint has never before warned about this. But now it is, saying:
JSLint 以前从未警告过这一点。但现在是,说:
Use the function form of "use strict".
使用“use strict”的函数形式。
Does anyone know what the "function form" would be?
有谁知道“函数形式”是什么?
回答by bdukes
Include 'use strict';
as the first statement in a wrapping function, so it only affects that function. This prevents problems when concatenating scripts that aren't strict.
Include'use strict';
作为包装函数中的第一条语句,因此它只影响该函数。这可以防止在连接不严格的脚本时出现问题。
See Douglas Crockford's latest blog post Strict Mode Is Coming To Town.
请参阅道格拉斯·克罗克福德 (Douglas Crockford) 的最新博客文章严格模式即将到来。
Example from that post:
该帖子中的示例:
(function () {
'use strict';
// this function is strict...
}());
(function () {
// but this function is sloppy...
}());
Update:In case you don't want to wrap in immediate function (e.g. it is a node module), then you can disable the warning.
更新:如果您不想在立即函数中包装(例如它是一个节点模块),那么您可以禁用警告。
For JSLint(per Zhami):
对于JSLint(每个Zhami):
/*jslint node: true */
For JSHint:
对于JSHint:
/*jshint strict:false */
or (per Laith Shadeed)
或(根据Laith Shadeed)
/* jshint -W097 */
To disable any arbitrary warning from JSHint, check the map in JSHint source code(details in docs).
要禁用来自 JSHint 的任何任意警告,请检查JSHint 源代码中的映射(文档中的详细信息)。
Update 2:JSHintsupports node:boolean
option. See .jshintrc
at github.
更新 2:JSHint支持node:boolean
选项。见.jshintrc
github。
/* jshint node: true */
回答by Zhami
If you're writing modules for NodeJS, they are already encapsulated. Tell JSLint that you've got node by including at the top of your file:
如果您正在为 NodeJS 编写模块,那么它们已经被封装了。通过在文件的顶部包含来告诉 JSLint 你有节点:
/*jslint node: true */
回答by Thorsten Lorenz
I'd suggest to use jshintinstead.
我建议改用jshint。
It allows to suppress this warning via /*jshint globalstrict: true*/
.
它允许通过/*jshint globalstrict: true*/
.
If you are writing a library, I would only suggest using global strict if your code is encapsulated into modules as is the case with nodejs.
如果您正在编写一个库,我只建议在您的代码像 nodejs 一样封装到模块中时使用 global strict。
Otherwise you'd force everyone who is using your library into strict mode.
否则你会强制每个使用你的库的人进入严格模式。
回答by qris
I started creating a Node.js/browserify application following the Cross Platform JavaScriptblog post. And I ran into this issue, because my brand new Gruntfile didn't pass jshint.
我开始在跨平台 JavaScript博客文章之后创建一个 Node.js/browserify 应用程序。我遇到了这个问题,因为我全新的 Gruntfile 没有通过 jshint。
Luckily I found an answer in the Leanpub book on Grunt:
幸运的是,我在关于 Grunt的Leanpub 书中找到了答案:
If we try it now, we will scan our Gruntfile… and get some errors:
$ grunt jshint Running "jshint:all" (jshint) task Linting Gruntfile.js...ERROR [L1:C1] W097: Use the function form of "use strict". 'use strict'; Linting Gruntfile.js...ERROR [L3:C1] W117: 'module' is not defined. module.exports = function (grunt) { Warning: Task "jshint:all" failed. Use --force to continue.
Both errors are because the Gruntfile is a Node program, and by default JSHint does not recognise or allow the use of
module
and the string version ofuse strict
. We can set a JSHint rule that will accept our Node programs. Let's edit our jshint task configuration and add an options key:jshint: { options: { node: true }, }
如果我们现在尝试,我们将扫描我们的 Gruntfile……并得到一些错误:
$ grunt jshint Running "jshint:all" (jshint) task Linting Gruntfile.js...ERROR [L1:C1] W097: Use the function form of "use strict". 'use strict'; Linting Gruntfile.js...ERROR [L3:C1] W117: 'module' is not defined. module.exports = function (grunt) { Warning: Task "jshint:all" failed. Use --force to continue.
这两种错误是因为Gruntfile是节点计划,并默认JSHint不承认或允许使用的
module
和的字符串版本use strict
。我们可以设置一个 JSHint 规则来接受我们的 Node 程序。让我们编辑我们的 jshint 任务配置并添加一个选项键:jshint: { options: { node: true }, }
Adding node: true
to the jshint options
, to put jshint into "Node mode", removed both errors for me.
添加node: true
到 jshint options
,将 jshint 置于“节点模式”,为我消除了这两个错误。
回答by Sahil Ahuja
Add a file .jslintrc (or .jshintrc in the case of jshint) at the root of your project with the following content:
在项目的根目录添加一个文件 .jslintrc(或 .jshintrc,如果是 jshint),内容如下:
{
"node": true
}
回答by glycoslave
There's nothing innately wrong with the string form.
字符串形式没有什么天生的错误。
Rather than avoid the "global" strict form for worry of concatenating non-strict javascript, it's probably better to just fix the damn non-strict javascript to be strict.
与其避免使用“全局”严格形式来担心连接非严格 javascript,不如将该死的非严格 javascript 修复为严格。
回答by Dirigible
I think everyone missed the "suddenly" part of this question. Most likely, your .jshintrc has a syntax error, so it's not including the 'browser' line. Run it through a json validator to see where the error is.
我认为每个人都错过了这个问题的“突然”部分。最有可能的是,您的 .jshintrc 有语法错误,因此它不包括“浏览器”行。通过 json 验证器运行它以查看错误在哪里。
回答by DB K
process.on('warning', function(e) {
'use strict';
console.warn(e.stack);
});
process.on('uncaughtException', function(e) {
'use strict';
console.warn(e.stack);
});
add this lines to at the starting point of your file
将此行添加到文件的起点
回答by Jason Stackhouse
This is how simple it is: If you want to be strict with all your code, add "use strict";
at the start of your JavaScript.
这就是多么简单:如果您想对所有代码严格,请"use strict";
在 JavaScript 的开头添加。
But if you only want to be strict with some of your code, use the function form. Anyhow, I would recomend you to use it at the beginning of your JavaScript because this will help you be a better coder.
但是,如果您只想对您的某些代码进行严格处理,请使用函数形式。无论如何,我建议您在 JavaScript 开始时使用它,因为这将帮助您成为更好的编码员。