Sub topic 7
Own Properties versus Inherited Properties
This chapter assumes you know how property lookup walks the prototype chain and how Object.create sets an object's prototype. Here you will learn to tell whether a property lives on the object itself or comes from further up the chain.
Two Kinds of Properties
An own property is stored directly on the object. An inherited property is found on some object in the prototype chain. A normal property read such as obj.x treats both kinds the same way. Some tasks need to tell them apart, for example serializing data, copying objects, or checking user input.
const base = {
eats: true,
walk() {
return 'walking';
}
};
const rabbit = Object.create(base);
rabbit.name = 'Rabbit';
console.log(Object.hasOwn(rabbit, 'name')); // true
console.log(Object.hasOwn(rabbit, 'eats')); // false
console.log('name' in rabbit); // true
console.log('eats' in rabbit); // true
console.log('toString' in rabbit); // true (from Object.prototype)
console.log(Object.keys(rabbit)); // ['name']The in operator searches the whole chain. Object.hasOwn looks only at the object itself.
Iteration: for...in versus Object.keys
for...in visits every enumerable property, both own and inherited. An enumerable property is one whose enumerable attribute is true. Methods and fields you write in an object literal are enumerable. The methods on Object.prototype, such as toString, are not, so they never show up.
const base = { eats: true, walk() {} };
const rabbit = Object.create(base);
rabbit.name = 'Rabbit';
for (const key in rabbit) {
console.log(key); // name, eats, walk
}
for (const key in rabbit) {
if (Object.hasOwn(rabbit, key)) {
console.log('own:', key); // own: name
}
}To work with own keys only, Object.keys, Object.values, and Object.entries are usually clearer than a filtered for...in loop.
| Check | Own | Inherited | Non-enumerable |
|---|---|---|---|
| `Object.hasOwn(obj, k)` | yes | no | yes |
| `k in obj` | yes | yes | yes |
| `for...in` | yes | yes | no |
| `Object.keys(obj)` | yes | no | no |
| `Object.getOwnPropertyNames(obj)` | yes | no | yes |
Why Object.hasOwn and Not hasOwnProperty
Older code calls obj.hasOwnProperty(k). That method is itself inherited from Object.prototype, so it can fail in two ways:
const dict = Object.create(null);
dict.apple = 3;
// dict.hasOwnProperty('apple'); // TypeError: not a function
const tricky = { hasOwnProperty: () => false, id: 1 };
console.log(tricky.hasOwnProperty('id')); // false (wrong)
console.log(Object.hasOwn(tricky, 'id')); // true
console.log(Object.hasOwn(dict, 'apple')); // trueCommon Mistakes
- Using
into check whether user data contains a key.'constructor' in {}istrue. - Looping with
for...inand expecting only own data, then picking up enumerable methods that a library or mixin added to a prototype. - Treating a property whose value is
undefinedas missing.Object.hasOwn({ a: undefined }, 'a')istrue.
Exercise
Given const p = { color: 'red' }; const c = Object.create(p); c.size = 2;, what do Object.keys(c), 'color' in c, Object.hasOwn(c, 'color'), and the keys printed by for (const k in c) return?
Show answer
Object.keys(c) is ['size']. 'color' in c is true. Object.hasOwn(c, 'color') is false. The for...in loop prints size and then color: own keys come first, followed by enumerable inherited keys.
Exercise
Why does Object.create(null).hasOwnProperty('x') throw, and what should you write instead?
Show answer
An object with a null prototype inherits nothing, so hasOwnProperty is undefined and calling it throws a TypeError. Use Object.hasOwn(obj, 'x') instead.