2

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.

mikey balls
  • 115
  • 1
  • 7
  • Can you post your JSLint config file? Also, there should be an actual error message associated with that line, which you should also post. – Paul Oct 03 '16 at 16:50
  • 2
    JSLint is an obsolete relic. Move on. There are **MUCH** better alternatives. If your company insists on using it, then convince them to change, or move on. Believe me, you have much better things to do with your time than fight with JSLint. –  Oct 03 '16 at 17:09
  • That's also true, +1 for @torazaburo, but since he can't ask for a library recommendation... – Paul Oct 03 '16 at 17:17
  • Can you provide the entire file? –  Oct 03 '16 at 18:25

2 Answers2

4

It looks like the answer is that JSLint believes instanceof can yield unintended/unexpected results.

Look at this, from instanceof's page at MDN:

var simpleStr = 'This is a simple string';
simpleStr instanceof String; // *returns false*, checks the prototype chain, finds undefined

That was unexpected! You have to initialize with String to get expected results.

var myString  = new String();
var newStr    = new String('String created with constructor');

myString  instanceof String; // returns true
newStr    instanceof String; // returns true

I believe the "right" answer for JSLint is to try the Object.prototype.toString.call trick described here, which would look like this:

"[object String]" === Object.prototype.toString.call("adsfsad");
"[object String]" === Object.prototype.toString.call(myString);
"[object String]" === Object.prototype.toString.call(newStr);

All true.

Crockford's said before that JSLint should help you prevent the use of idioms that look like errors, or code that produces unexpected results. That's what's going on here, at the expense of some performance. Though remember not to fret with micro-optimizations!

ruffin
  • 16,507
  • 9
  • 88
  • 138
1

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.

Paul
  • 35,689
  • 11
  • 93
  • 122
  • I don't want to surpress it, I want to understand why it is there. – mikey balls Oct 03 '16 at 17:02
  • So you've given an answer to a question you can't reproduce and don't understand, which consists of random guesses that you are asking the OP to try. If you believe that the question is not reproducible, or does not contain all the necessary information, then leave a comment, or those are also close reasons. –  Oct 03 '16 at 17:29
  • 2
    That may be a bit harsher than you intended, @torazaburo. I would say my suggestions are at worst educated guesses, and I'm so far the only one trying to help him. I'm sorry you don't feel it's useful to try. – Paul Oct 03 '16 at 17:56
  • 2
    @Paul - the parentheses thing worked. This is my last time using jslint. It is making my code look stupid. – mikey balls Oct 03 '16 at 22:07
  • @mikey balls yeah, I use other tools most of the time now. Code style checks as a concept aren't bad, but implementation varies. – Paul Oct 03 '16 at 23:25