简体   繁体   中英

JSLint: “'HTMLElement' was used before it was defined.”

JSLint: "'HTMLElement' was used before it was defined."

if (element instanceof HTMLElement)

How do I fix this?

Do I have to add an exception or ignore it?

Check "Tolerate misordered definitions".

This works for me if my entire script is:

var e;
if (e instanceof HTMLElement) {
    alert("");
}

and the only checked box is "Tolerate misordered definitions".

The response I get is:

Global HTMLElement, alert, e

This checkbox only seems to apply to identifiers used in the global scope. If this is tried within a function body, JSLint will complain about alert unless you check the box to "Assume console, alert". However the following trick does satisfy JSLint:

var HTMLElement = HTMLElement;
(function () {
    var e;
    if (e instanceof HTMLElement) {
        alert("");
    }
}());

This passes with checkboxes "Assume console, alert", "Tolerate misordered definitions", and "Tolerate missing use strict." I get the response:

Global HTMLElement  
3 'anonymous'()
    Variable e
    Global HTMLElement
    Complexity 2

Definitely a hack; /*global HTMLElement */ is best. Makes sense, though, after reading the JSLint instructions.

Looks like you'll have to add an exception for it. I could not find any of the checkbox options that remove the error.

You can also add HTMLElement to the predefined textbox at the bottom of the JSLint page (if you are using the online validation version).

Since I'm assuming you are in a browser this should be a valid exclusion.

将“ alert”更改为“ window.alert”,并在顶部使用jslint指令/ * jslint browser:true * /。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM