【问题标题】:JSHint and issue with underscore '_'JSHint 和下划线 '_' 的问题
【发布时间】:2023-03-10 12:07:01
【问题描述】:

运行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
    }
}

【问题讨论】:

  • Redefinition 建议您已经在某处定义了它
  • 您确定正在解析 jshint 配置文件吗?尝试将 "someglobal" : true 添加到全局变量,然后执行 var someglobal = 123;它是否引发了重新定义 someglobal 的异常?

标签: node.js jshint lodash


【解决方案1】:

将 .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
  }
}

【讨论】:

    【解决方案2】:

    解决此问题的更好(且常用)方法是使用双下划线__,同时需要underscore 或lodash 库。无需为此更改jshintrc 文件。

    【讨论】:

      猜你喜欢
      • 2014-04-06
      • 2011-10-23
      • 2011-07-10
      • 2018-10-16
      • 1970-01-01
      • 1970-01-01
      • 2019-09-19
      • 2019-09-07
      • 1970-01-01
      相关资源
      最近更新 更多