How to rectify “Document was used before it was defined” using Jslint

↘锁芯ラ 提交于 2019-12-03 05:29:05
penartur

Place

/*jslint browser:true */

in the beginning of the body of your function. Or, alternatively, you may use

/*global document: false */

JSLint is for the checking of any javascript code, and the document global object does not exists everywhere, so you have to manually tell JSLint that your code is aimed to be executed in browser, and thus document global variable is defined. For example, for server-side javascript it is expected for JSLint to report about this error.

stanton

From jslint's website, you can read about a /global/ directive that allows you to set variables that are assumed to be declared elsewhere.

Here is an example (put this at the top of the file):

/\*global var1,var2,var3,var4,var5\*/

or, in starjava's case:

/\*global document\*/

I'm not actually sure if the :true or :false is needed, but it looks like it's recommended from what I read on the site. http://www.jslint.com/lint.html

Make sure the initial global statement is on the same line as '/\*', or else it breaks.

if you are using node can add the following line

/ * node JSLint: true * /

at the start of the script http://jslinterrors.com/a-was-used-before-it-was-defined/

If all your JS code is supposed to be run from browser then just the add the --browser flag.

jslint --browser my.js

In cli mode you can use config file:
jslint.conf

{
    "predef": [
        "module",
        "require",
        "window",
        "document",
        "jQuery",
        "Backbone",
        "Marionette",
        "Modernizr",
        "_",
        "setTimeout",
        "clearTimeout",
        "setInterval",
        "clearInterval"
    ],
    "evil":false,
    "indent":4,
    "vars":true,
    "passfail":false,
    "plusplus":false
}

And use jslint with --config jslint.conf parameter.

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