1

Why jshint is not reporting forin (hasOwnProperty) error for the following code? jslint do report error on it but jshint doesn't.

/*jshint forin: true */

(function () {
    "use strict";

    var obj = {a: 1, b: 2}, i = null;

    for (i in obj) {
        if (i === 0) {
            console.log('blah...');
        }
    }
}());
iFadey
  • 320
  • 3
  • 11

1 Answers1

2

Here's the relevant snippet of code from JSHint (modified slightly for formatting):

if (
    state.option.forin && 
    s && 
    (s.length > 1 || typeof s[0] !== "object" || s[0].value !== "if")
) {
    warning("W089", this);
}

The important part is s[0].value !== "if". JSHint won't raise an error if the first statement of the for...in body is an if statement, regardless of the condition of that statement.

James Allardice
  • 164,175
  • 21
  • 332
  • 312
  • Oh I see! It's not a bug, it's by design. Thanks a lot for the answer :) – iFadey Jun 03 '13 at 08:58
  • Good call. Just to round out why JSLint *does* work, [JSLint's current code](https://github.com/douglascrockford/JSLint/blob/c657984cd7dfc17277feadb86d1de24c664f944a/jslint.js) has that explicit check for `hasOwnProperty` for the `if` after the `for` (rather than JSHint's only checking for the `if`) at the `case` block that starts line 3687. – ruffin Jun 04 '13 at 01:48