Skip to content

Commit

Permalink
Docs: Elaborate on guard-for-in best practice (fixes #7071) (#7094)
Browse files Browse the repository at this point in the history
Explains why a naive `foo.hasOwnProperty(key)` call is a bad idea in the `guard-for-in` page.

Hopefully prevents future users from being as confused as I was :) (or, maybe worse, ignoring the obscure `{}.hasOwnProperty.call(foo, key)` syntax)
  • Loading branch information
dallonf authored and platinumazure committed Sep 10, 2016
1 parent 58e6d76 commit 0d50943
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions docs/rules/guard-for-in.md
Expand Up @@ -8,6 +8,8 @@ for (key in foo) {
}
```

Note that simply checking `foo.hasOwnProperty(key)` is likely to cause an error in some cases; see [no-prototype-builtins](no-prototype-builtins.md).

## Rule Details

This rule is aimed at preventing unexpected behavior that could arise from using a `for in` loop without filtering the results in the loop. As such, it will warn when `for in` loops do not filter their results with an `if` statement.
Expand All @@ -34,6 +36,10 @@ for (key in foo) {
}
```

## Related Rules

* [no-prototype-builtins](no-prototype-builtins.md)

## Further Reading

* [Exploring JavaScript for-in loops](http://javascriptweblog.wordpress.com/2011/01/04/exploring-javascript-for-in-loops/)
Expand Down

0 comments on commit 0d50943

Please sign in to comment.