JSHint

JSHint 是一个开源工具,可以检测 JavaScript 代码中的错误和潜在问题。

要提示你的 JavaScript,你有两种选择。

  1. 转到 JSHint.com 并将代码粘贴到行文本编辑器中。
  2. 在 IDE 中安装 JSHint

将它添加到 IDE 的好处是,你可以创建一个名为 .jshintrc 的 JSON 配置文件,该文件将在 linting 你的程序时使用。如果要在项目之间共享配置,这是修道院。

示例 .jshintrc 文件

{
    "-W097": false, // Allow "use strict" at document level
    "browser": true, // defines globals exposed by modern browsers http://jshint.com/docs/options/#browser
    "curly": true, // requires you to always put curly braces around blocks in loops and conditionals http://jshint.com/docs/options/#curly
    "devel": true, // defines globals that are usually used for logging poor-man's debugging: console, alert, etc. http://jshint.com/docs/options/#devel
    // List global variables (false means read only)
    "globals": {
        "globalVar": true
    },
    "jquery": true, // This option defines globals exposed by the jQuery JavaScript library.
    "newcap": false,
    // List any global functions or const vars
    "predef": [
        "GlobalFunction",
        "GlobalFunction2"
    ],
    "undef": true, // warn about undefined vars
    "unused": true // warn about unused vars
}

JSHint 还允许配置特定的行/代码块

switch(operation)
{
   case '+'
   {
      result = a + b;
      break;
   }

   // JSHint W086 Expected a 'break' statement
   // JSHint flag to allow cases to not need a break
   /* falls through */
   case '*':
   case 'x':
   {
      result = a * b;
      break;
   }
}

// JSHint disable error for variable not defined, because it is defined in another file
/* jshint -W117 */
globalVariable = 'in-another-file.js';
/* jshint +W117 */

http://jshint.com/docs/options/ 中记录了更多配置选项。