JSHint和下划线问题' _'

时间:2014-11-28 12:00:07

标签: node.js jshint lodash

运行jshint时出现以下错误;

line 4   col 5  Redefinition of '_'.

代码抱怨是;

var _ = require('lodash');

项目中的jshint

{
    "node": true,
    "esnext": true,
    "bitwise": true,
    "eqeqeq": true,
    "immed": true,
    "latedef": "nofunc",
    "newcap": true,
    "noarg": true,
    "regexp": true,
    "undef": true,
    "smarttabs": true,
    "asi": true,
    "debug": true,
    "globals": {
        "angular": false,
        "_": false
    }
}

2 个答案:

答案 0 :(得分:3)

将.jshintrc更新为以下似乎解决了问题。

{
    "node": true,
    "browser": true,
    "esnext": true,
    "bitwise": false,
    "camelcase": false,
    "eqeqeq": true,
    "immed": true,
    "indent": 4,
    "latedef": true,
    "newcap": true,
    "noarg": true,
    "quotmark": "single",
    "regexp": true,
    "undef": true,
    "unused": false,    
    "strict": true,
    "trailing": true,
    "smarttabs": true,
    "globals": {
        "jQuery": true,
        "angular": true,
        "console": true,
        "AppConfig": true,
        "$": true,
        "_": true,
        "moment": true,
        "module": true,
        "inject": true,
        "browser": true,
        "element": true,

        "describe": true,
        "before": true,
        "beforeEach": true,
        "after": true,
        "afterEach": true,
        "expect": true,
        "it": true,
        "by": true,
        "chai": true
    }
}

还创建了一个.jshintrc-spec

{
  "extends": ".jshintrc",
  "globals": {
    "describe": true,
    "it": true,
    "before": true,
    "beforeEach": true,
    "after": true,
    "afterEach": true,
    "chai": true,
    "inject" : true
  }
}

答案 1 :(得分:0)

更好(和常用)解决此问题的方法是在需要__underscore库时使用双下划线lodash。无需为此更改jshintrc文件。