【问题标题】:How to fix '$' was used before it was defined in both jslint and jshint using coding?在使用编码在 jslint 和 jshint 中定义之前,如何修复“$”?
【发布时间】:2014-10-17 02:35:00
【问题描述】:

我正在使用 jQUery 。而在 jslint 中,我继续

'$' was used before it was defined.

document was used before it was defined.

我知道我可以使用

阻止这些人出现
/*jslint browser: true*/
/*global $, jQuery*/

但如果可能的话,我实际上想通过编码解决这个问题。所以,我以前确实喜欢。

var $, document;
$(document).ready(function () {
    "use strict";

但是,现在它说

Two warnings
1   Redefinition of '$'.
1   Redefinition of 'document'.

jshint 中。有没有我可以用来取悦 jshint 和 jslint 的编码?

【问题讨论】:

  • 我认为 jslint 无法知道该变量是由外部库定义的。这就是评论的目的,告诉它你知道它不知道的事情。
  • 问题是 - 代码是否正确?如果不是,你应该改变它。如果正确,则无需修复任何问题,因此添加适当的 cmets 就足够了。

标签: jquery jslint jshint


【解决方案1】:

您不能也不必通过编码来修复它。 $ 实际上作为全局变量存在于不同的文件中。这正是/*global ... */ 的用途。

【讨论】:

    【解决方案2】:

    请使用 jslint cmets。以下建议使用了一些技巧。在你的代码中有技巧不会有助于可维护性。这是code

    var myApplication = function () {
        "use strict";
        var $ = this.$, document = this.document, console = this.console;
        $(document).ready(function () {
            //...
            console.log("test");
        });
    };
    
    myApplication.call(this);
    

    【讨论】:

      猜你喜欢
      • 2012-03-26
      • 2013-08-17
      • 2012-10-09
      • 2014-06-07
      • 1970-01-01
      • 2013-12-18
      • 2017-07-18
      相关资源
      最近更新 更多