JSLint will throw the "The body of a for in should be wrapped in an if statement to filter unwanted properties from the prototype" error when it encounters a for-in statement in which the first statement is not an if statement which contains a call to the hasOwnProperty method. JSHint is a little more leniant and will throw this error when the first statement of the for-in statement is not an if statement, and the forin option is set to true. Here's an example in which we attempt to enumerate the properties of an object:
This error is raised to highlight bad practice and code that may not work as you expect it to. Your code may run without error, depending on whether you've extended the prototype of the object you're trying to enumerate, or, for example, Object.prototype, but it's bad practice and could lead to problems in the future.
The for-in construct will enumerate all enumerable properties of an object, whether they belong to the object itself or an object in its prototype chain. Consider the following example, in which we add a completely useless random method to Object.prototype:
After the above snippet has executed, all objects in our script will have access to that random method (via their prototype chain, all the way down to Object.prototype). Since we have not defined our method as non-enumerable (which is only possible in ECMAScript 5 environments that support the Object.create and Object.defineProperty methods), it will be enumerated along with any other enumerable properties by a for-in loop.
However, all objects also inherit a method called hasOwnProperty, which can be used to check whether or not an object has a property with a given identifier that belongs to it and not to its prototype, or any objects down its prototype chain (ES5 §126.96.36.199):
3. Let desc be the result of calling the [[GetOwnProperty]] internal method of O passing P as the argument.
4. If desc is undefined, return false.
5. Return true.
The "[[GetOwnProperty]] internal method" (ES5 §8.12.1) returns a value that tells the engine whether or not the object in question has an "own property" with the given identifier. And "own property" is defined as follows (ES5 §4.3.30):
property that is directly contained by its object
In our example, since the random method would be accessible (via inheritance) to the me object, but isn't an "own property" of it, we would need to use the hasOwnProperty method to ensure it's not enumerated:
In JSHint 1.0.0 and above you have the ability to ignore any warning with a special option syntax. The identifier of this warning is W089. This means you can tell JSHint to not issue this warning with the
/*jshint -W089 */ directive.