我附上以下声明:

"use strict";

在大多数Javascript文件的开头。

JSLint以前从未对此发出过警告。但现在它说:

使用“Use strict”的函数形式。

有人知道函数形式是什么吗?


当前回答

在你的项目的根目录下添加一个文件.jslintrc(在jshint的情况下是.jshintrc),内容如下:

{
    "node": true
}

其他回答

在你的项目的根目录下添加一个文件.jslintrc(在jshint的情况下是.jshintrc),内容如下:

{
    "node": true
}

我想大家都忽略了这个问题中“突然”的部分。很可能,你的.jshintrc有一个语法错误,所以它不包括'browser'行。通过json验证器运行它,查看错误在哪里。

如果你正在为NodeJS编写模块,它们已经被封装了。告诉JSLint你已经在你的文件顶部包含了node:

/*jslint node: true */

我开始创建一个Node.js/browserify应用程序在跨平台JavaScript博客文章之后。我遇到了这个问题,因为我的全新Gruntfile没有通过jshint。

幸运的是,我在Leanpub关于Grunt的书中找到了答案:

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 of use 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 }, }

添加节点:true的jshint选项,把jshint进入“节点模式”,为我消除了这两个错误。

process.on('warning', function(e) {
    'use strict';
    console.warn(e.stack);
});
process.on('uncaughtException', function(e) {
    'use strict';
    console.warn(e.stack);
});

将这些行添加到文件的起始点