Search code examples
javascriptjslint

How can I use jslint and instanceof?


I'm getting an "error" on this line:

var test = selector instanceof Priv.Constructor;

The error is:

`Unexpected 'instanceof'.`

I have minimal flags set:

/*jslint
    browser
    this
*/

/*global
    window
*/

and could find anything on why here:

http://jslint.com/help.html

I don't want to supress the warning, I want to understand why it is there.


Solution

  • In your jslint.conf file, set the "expr" option to true to suppress that warning:

    {
        "evil":false,
        "indent":2,
        "vars":true,
        "passfail":false,
        "plusplus":false,
        "predef": "module,require",
        "expr" : true
    }
    

    // Update for question

    As I understand it, it's because you're using it as an assignment in line. While my local copy of JSLint isn't throwing that error for it, I can imagine that it's something like a dangling expression assignment. Try wrapping the expression in parentheses to make sure JSLint doesn't think it's dangling, e.g.

    var test = (selector instanceof Priv.Constructor);
    

    And see if that fixes it. If not, see if you get the error with a standalone check w/o assignment, eg:

    if(selector instanceof Priv.Constructor){ console.log('it is an instance');}
    

    Finally, it may well be something earlier in your code that's broken, and it's just that it didn't get something that should've closed the previous statement before it got to the instanceof in which case the "wrong" error is being thrown from your perspective.