Skip to content

Commit 5251a92

Browse files
authored
docs: Describe guard options for guard-for-in (#16986)
Describes the different checks that can be used to satisfy guard-for-in and links prefer-object-has-own as a related rule.
1 parent 69bc0e2 commit 5251a92

1 file changed

Lines changed: 5 additions & 0 deletions

File tree

docs/src/rules/guard-for-in.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
title: guard-for-in
33
rule_type: suggestion
44
related_rules:
5+
- prefer-object-has-own
56
- no-prototype-builtins
67
further_reading:
78
- https://javascriptweblog.wordpress.com/2011/01/04/exploring-javascript-for-in-loops/
@@ -17,6 +18,10 @@ for (key in foo) {
1718
}
1819
```
1920

21+
For codebases that do not support ES2022, `Object.prototype.hasOwnProperty.call(foo, key)` can be used as a check that the property is not inherited.
22+
23+
For codebases that do support ES2022, `Object.hasOwn(foo, key)` can be used as a shorter alternative; see [prefer-object-has-own](prefer-object-has-own).
24+
2025
Note that simply checking `foo.hasOwnProperty(key)` is likely to cause an error in some cases; see [no-prototype-builtins](no-prototype-builtins).
2126

2227
## Rule Details

0 commit comments

Comments
 (0)