How to fix '$' was used before it was defined in both jslint and jshint using coding?

浪子不回头ぞ 提交于 2019-12-02 15:40:51

问题


I am using jQUery . And in jslint, I keep on

'$' was used before it was defined.

or

document was used before it was defined.

I know I can stop those from showing up by using

/*jslint browser: true*/
/*global $, jQuery*/

But I wanted to actually fix this through coding if possible. So, I did like before.

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

However, now it says

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

in jshint. Is there a coding that I can use to please both jshint and jslint?


回答1:


Please use jslint comments. The following suggestion uses a few tricks. Having tricks in your code won't help for maintainability. Here is the code:

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

myApplication.call(this);



回答2:


You cannot, and don't have to, fix it by coding. $ does in fact exist as global variable in a different file. That's exactly what /*global ... */ is for.



来源:https://stackoverflow.com/questions/25460567/how-to-fix-was-used-before-it-was-defined-in-both-jslint-and-jshint-using-co

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!