|
| 1 | +<!--- |
| 2 | +{ |
| 3 | + "titles": [ |
| 4 | + "forin" |
| 5 | + ], |
| 6 | + "slugs": [ |
| 7 | + "option-forin" |
| 8 | + ], |
| 9 | + "linters": [ |
| 10 | + "jslint", |
| 11 | + "jshint" |
| 12 | + ], |
| 13 | + "author": "jallardice", |
| 14 | + "subject": "option" |
| 15 | +} |
| 16 | +--> |
| 17 | + |
| 18 | +### What does this option do? |
| 19 | + |
| 20 | +In JSLint the `forin` option is used to allow the usage of unfiltered for-in |
| 21 | +statements. Since the for-in statement will enumerate properties from the |
| 22 | +prototype chain and not just from the object in question, it can potentially |
| 23 | +cause problems if other code is modifying native prototypes without your |
| 24 | +knowledge. In the following example we use a for-in loop to iterate over the |
| 25 | +elements of an array. This is commonly called out as bad practice because |
| 26 | +enumerable methods added to `Array.prototype` will be produced: |
| 27 | + |
| 28 | +<!--- |
| 29 | +{ |
| 30 | + "linter": "jslint" |
| 31 | +} |
| 32 | +--> |
| 33 | +```javascript |
| 34 | +/*jslint forin: true */ |
| 35 | +/*global doSomething */ |
| 36 | + |
| 37 | +var arr = [], |
| 38 | + i; |
| 39 | + |
| 40 | +for (i in arr) { |
| 41 | + doSomething(arr[i]); |
| 42 | +} |
| 43 | +``` |
| 44 | + |
| 45 | +The JSHint `forin` option is used to **require** the filtering of such loops. |
| 46 | +Here's the same example again: |
| 47 | + |
| 48 | +<!--- |
| 49 | +{ |
| 50 | + "linter": "jshint" |
| 51 | +} |
| 52 | +--> |
| 53 | +```javascript |
| 54 | +/*jshint forin: true */ |
| 55 | +/*global doSomething */ |
| 56 | + |
| 57 | +var arr = [], |
| 58 | + i; |
| 59 | + |
| 60 | +for (i in arr) { |
| 61 | + doSomething(arr[i]); |
| 62 | +} |
| 63 | +``` |
| 64 | + |
| 65 | +A "filtered" for-in statement is one that ensures only properties that belong to |
| 66 | +the object in question are operated on. This is usually achieved by wrapping the |
| 67 | +body of the for-in in an `if` statement that makes sure each property is an |
| 68 | +"own" property of the object: |
| 69 | + |
| 70 | +<!--- |
| 71 | +{ |
| 72 | + "linter": "jshint" |
| 73 | +} |
| 74 | +--> |
| 75 | +```javascript |
| 76 | +/*jshint forin: true */ |
| 77 | +/*global doSomething */ |
| 78 | + |
| 79 | +var arr = [], |
| 80 | + i; |
| 81 | + |
| 82 | +for (i in arr) { |
| 83 | + if (arr.hasOwnProperty(i)) { |
| 84 | + doSomething(arr[i]); |
| 85 | + } |
| 86 | +} |
| 87 | +``` |
| 88 | + |
| 89 | +### When should I use this option? |
| 90 | + |
| 91 | +With JSLint, if the `forin` option is not set, you'll get a "[The body of a for |
| 92 | +in should be wrapped in an if statement to filter unwanted properties from the |
| 93 | +prototype][forin]" error any time an unfiltered for-in statement is used. In |
| 94 | +JSHint the opposite is true and you'll receive the same error message for each |
| 95 | +unfiltered for-in when the option *is* set. |
| 96 | + |
| 97 | +Note that in JSHint this is an *enforcing* option which means JSHint does not |
| 98 | +apply it by default. If you do not explicitly set this option to `true` JSHint |
| 99 | +will allow the use of bitwise operators anywhere in your code. |
| 100 | + |
| 101 | +#### Recommendation |
| 102 | + |
| 103 | + - **JSLint** - Do not set this option (you will not be able to use unfiltered |
| 104 | + for-in statements). |
| 105 | + |
| 106 | + - **JSHint** - Set this option to `true` (you will not be able to use unfiltered |
| 107 | + for-in statements). |
| 108 | + |
| 109 | +[forin]: /the-body-of-a-for-in-should-be-wrapped-in-an-if-statement |
0 commit comments