Sub topic 4
Inherited Properties and Filtering with Object.hasOwn
This chapter assumes you know how for...in visits enumerable string keys, and that you have a rough idea of prototypes: every object can link to another object and borrow its properties.
for...in Follows the Prototype Chain
for...in does more than list an object's own keys. It also walks up the prototype chain and visits every enumerable property it finds on the way. Methods on Object.prototype don't show up, because built-in methods are non-enumerable. Properties you add to a prototype yourself are a different matter.
const base = { shared: true };
const obj = Object.create(base);
obj.own = 1;
for (const key in obj) {
console.log(key, Object.hasOwn(obj, key));
}
// own true
// shared falseobj has one own property, own. The key shared lives on base, but for...in reports it as well. Own keys come first, then the keys of each prototype in turn. A key that appears on more than one level is reported only once.
Filtering with Object.hasOwn
Object.hasOwn(obj, key) returns true only when key is an own property of obj. Put it inside the loop to skip inherited keys:
function ownEntries(obj) {
const result = [];
for (const key in obj) {
if (!Object.hasOwn(obj, key)) continue;
result.push([key, obj[key]]);
}
return result;
}
const defaults = { theme: 'dark' };
const settings = Object.create(defaults);
settings.fontSize = 14;
console.log(ownEntries(settings)); // [['fontSize', 14]]Why Not hasOwnProperty?
Older code calls obj.hasOwnProperty(key). That call fails in two common cases:
const dict = Object.create(null);
dict.a = 1;
// dict.hasOwnProperty('a'); // TypeError: no such method
const shadowed = { hasOwnProperty: () => false, a: 1 };
console.log(shadowed.hasOwnProperty('a')); // false (wrong)
console.log(Object.hasOwn(dict, 'a')); // true
console.log(Object.hasOwn(shadowed, 'a')); // trueAn object made with Object.create(null) has no prototype, so it has no hasOwnProperty method. An object can also define its own property named hasOwnProperty that hides the real one. Object.hasOwn is a static function, so neither problem can affect it. It arrived in ES2022. In older environments, use Object.prototype.hasOwnProperty.call(obj, key) instead.
Common Errors
| Mistake | Effect | Fix |
|---|---|---|
| No own-property check in `for...in` | Inherited keys leak into results | Add `Object.hasOwn` or use `Object.keys` |
| `obj.hasOwnProperty(k)` on null-prototype objects | `TypeError` | Use `Object.hasOwn(obj, k)` |
| Assuming `in` means "own" | `'shared' in obj` is `true` for inherited keys | Use `Object.hasOwn` when ownership matters |
| Adding enumerable properties to `Object.prototype` | Every `for...in` everywhere sees them | Never do this. If you must, use `Object.defineProperty` with `enumerable: false` |
Exercise
Given const parent = { a: 1 }; const child = Object.create(parent); child.b = 2;, what does for (const k in child) console.log(k); print, and how would you change the loop so it prints only b?
Show answer
It prints b, then a. Own keys come first, then the inherited enumerable key from parent. To print only b, add a filter: for (const k in child) { if (Object.hasOwn(child, k)) console.log(k); }. You could also use for (const k of Object.keys(child)) console.log(k);.
Exercise
Why does Object.hasOwn(dict, 'x') work on const dict = Object.create(null) when dict.hasOwnProperty('x') throws?
Show answer
dict has no prototype, so it doesn't inherit a hasOwnProperty method, and calling it throws a TypeError. Object.hasOwn is a static function on Object that takes the target as an argument, so it never looks anything up on dict's prototype chain.