废话不多说,先上github:https://github.com/jshint/jshint
JSHint是一款用来检测你的js代码是否规范的软件,有了它你可以在刷新浏览器之前就知道你哪里写错了,由于可以集成插件到编辑器里,可以随时随地的调用快捷键来检查你的js代码规范(比如我用的sublime就是Ctrl+shift+J
)。
注意:它的使用依赖于nodejs,所以一定要安装了nodejs才可以正常的使用。
安装方法(十分全面,具体到每一个编辑器里如何集成都有说明):http://jshint.com/install/
关于sublime的JSHint插件
首先package control里面有很多关于jshint的插件(jslint就不要用了,比较老了,而且有插件但是找不到配置选项的文档),SublimeLinter-jshint不是很好安装,根据官方提供的选择,只有Sublime-JSHint
Gutter是可以在sublime3上使用的,那就是它了。
如何不报”use strict”错误
总是报这个错误是让人很反感的,毕竟有很多人连最基本的代码规范都没有,所以要设置一下
通常安装的插件都会有设置选项,个人还是比较喜欢sublime的设置选项的,比较适合懂编程的人来操作,以下还是以sublime为例:
首先第一次使用的时候它有可能提示你找不到nodejs,然后会弹出一个文件,里面可以设置文件路径,虽然我安装了nodejs,但是目录的名字其实是叫node的,所以安装对了也有可能要改一下这里的
{
// Simply using `node` without specifying a path sometimes doesn't work :(
// https://github.com/victorporof/Sublime-JSHint#oh-noez-command-not-found
// http://nodejs.org/#download
"node_path": {
"windows": "C:/Program Files/nodejs/node.exe",
"linux": "/usr/bin/node",
"osx": "/usr/local/bin/node"
},
//.........
}
接下来是设置文件,打开package settings->JSHint Gutter->Linting preference,会打开一个设置文件,这里面是关于检测规则的,找到其中的strict选项(至于选项的文档,你要是懒得找就点这里:http://jshint.com/docs/options/):
{
// The plugin looks for a .jshintrc file in the same directory as the source
// file you're prettifying (or any directory above if it doesn't exist, or in
// your home folder if everything else fails) and uses those options along
// the default ones.
// Details: https://github.com/victorporof/Sublime-JSHint#using-your-own-jshintrc-options
// Example: https://github.com/jshint/jshint/blob/master/examples/.jshintrc
// Documentation: http://www.jshint.com/docs/options/
"browser": true,
"esnext": true,
"globals": {},
"strict": "implied", //默认这里是global,改为implied即可忽略"use strict"报错
"undef": true,
"unused": true
}
接下来你就可以清爽优雅的使用Ctrl+shift+J
来检测你的JS代码规范了。