Skip to content

Commit c68fa78

Browse files
author
Walle Cyril
authored
Reflect.has is like the in operator
add iteration techniques
1 parent 1f22fb3 commit c68fa78

1 file changed

Lines changed: 15 additions & 2 deletions

File tree

js/Object.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,29 @@ Object.entries(anObject).forEach(function ([key, value]) {
5454
// iterate over the object and its full prototype chain
5555
let key;
5656
for (key in anObject) {
57-
anObject[key]; // value
57+
const value = anObject[key];
5858
}
5959

60+
// iterate over all own Properties including Symbols and non-enumerables (anti-pattern)
61+
Reflect.ownKeys(anObject).forEach(function (key) {
62+
const value = anObject[key];
63+
});
64+
65+
// iterate over own Properties including non-enumerables (anti-pattern)
66+
Object.getOwnPropertyNames(anObject).forEach(function (key) {
67+
const value = anObject[key];
68+
});
69+
6070
// has a key
6171
anObject.hasOwnProperty("key");
6272

6373
// has safe, works even when anObject has a key "hasOwnProperty"
6474
// also works for Objects without prototype
6575
Object.prototype.hasOwnProperty.call(anObject, "key");
66-
Reflect.has(anObject, "key")
76+
77+
// has a key, or it can be found in the prototype chain
78+
"key" in anObject;
79+
Reflect.has(anObject, "key");
6780

6881
// remove a value
6982
anObject["key"] = undefined;

0 commit comments

Comments
 (0)