簡體   English   中英

jshint錯誤:重新定義'$'

[英]jshint error: Redefinition of '$'

我有一個Node.js Web應用程序,我使用Backbone.js和jQuery。 我的主要js文件是app.js ,其中包含所有必需的腳本,包括jQuery。 所以,這是我的app.js代碼的開頭:

'use strict';

var _ = require('lodash');
var $ = require('jquery');
var B = require('backbone');
B.$ = $;

現在,如果我在我的項目上運行grunt ,它會在jQuery加載到$的行引發錯誤。 它告訴我這個:

Linting public/js/app.js ...ERROR
[L4:C5] W079: Redefinition of '$'.
var $ = require('jquery');

我仍然可以使用grunt --force運行所有內容,但無論如何我想消除此錯誤。 有人可以解釋為什么它會引發錯誤以及如何解決它?


我的.jshintrc文件:

{
    "laxcomma": true
    ,"laxbreak": true
    ,"curly": true
    ,"eqeqeq": true
    ,"immed": true
    ,"latedef": true
    ,"newcap": true
    ,"noarg": true
    ,"sub": true
    ,"undef": true
    ,"unused": false
    ,"asi": true
    ,"boss": true
    ,"eqnull": true
    ,"node": true
    ,"browser": true
    ,"jquery": true
    ,"predef":
    [
        "suite"
        ,"test"
        ,"setup"
        ,"teardown"
        ,"suiteSetup"
        ,"suiteTeardown"
        ,"requestAnimationFrame"
    ]
}

在jshint docs中: http//www.jshint.com/docs/options/

jquery = true 
This option defines globals exposed by the jQuery JavaScript library.

你告訴jshint jquery存在,因此他認為$是定義的。 刪除"jquery" : true" ,你的問題應該消失。

在文件頂部添加此項以使該錯誤消失:

/* jshint -W079 */

這里發生的是JQuery定義$變量,因此JSHint將其視為“潛在危險代碼”

一個更好的解決方案是直接需要jquery,這可以讓你全局訪問$ variable。

隨機猜測:你在其他地方包含jquery嗎? 例如在一些html文件中作為腳本。 或者其他一些腳本可能正在定義全局jquery變量。

確保在.jshintrc中沒有將'$'設置為預定義。

// Predefined Globals
"predef" : ["$"]   

如果是,請刪除。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM