Sub topic 1
Objects and Property Lookup
Inheritance in JavaScript starts with a simple question: when you write obj.prop, where does the engine look for prop? This chapter answers the first part of that question and introduces the hidden link that the rest of the tutorial builds on.
Prerequisites
This chapter assumes you are comfortable with:
- JavaScript object literals and property access with dot and bracket notation
- Functions, and how
thisis bound in method calls - ES2015 syntax such as
let,const, arrow functions, and template literals - Running JavaScript in a browser console or Node.js
Objects Are Collections of Properties
An object is a set of properties, and each property is a key paired with a value. Reading a property is called a property lookup.
const user = { name: 'Ada', age: 36 };
console.log(user.name); // "Ada"
console.log(user['age']); // 36
console.log(user.email); // undefinedProperties you define directly on an object are its own properties. Here user has two: name and age. A lookup for a key that doesn't exist does not throw. It returns undefined.
Now try something that should fail, but doesn't:
const user = { name: 'Ada', age: 36 };
console.log(user.toString()); // "[object Object]"
console.log(Object.hasOwn(user, 'toString')); // false
console.log(Object.keys(user)); // ['name', 'age']user has no own toString property, but the call still works. The method has to come from somewhere else.
Every Object Has a Hidden Link: [[Prototype]]
Every object has an internal slot called [[Prototype]]. The double brackets mark it as internal: it is defined by the language specification and you cannot read it with normal property access. Its value is either another object or null.
When a lookup fails to find an own property, the engine follows [[Prototype]] and searches the linked object. For an object literal, that linked object is Object.prototype, which is where toString lives.
You can inspect the link with Object.getPrototypeOf:
const user = { name: 'Ada' };
const list = [1, 2, 3];
console.log(Object.getPrototypeOf(user) === Object.prototype); // true
console.log(Object.getPrototypeOf(list) === Array.prototype); // true
console.log(Object.hasOwn(Object.prototype, 'toString')); // trueCommon Mistakes
| Mistake | What actually happens |
|---|---|
| Assuming `obj.prototype` gives an object's prototype | For a plain object it returns `undefined`. Use `Object.getPrototypeOf(obj)` instead. |
| Treating a successful lookup as proof of an own property | The value may have been found through `[[Prototype]]`. Check with `Object.hasOwn`. |
| Expecting `Object.keys` to list inherited methods | `Object.keys` returns only own enumerable keys. |
Exercise
Given const point = { x: 1 };, what do point.hasOwnProperty('x') and Object.hasOwn(point, 'hasOwnProperty') return, and why?
Show answer
point.hasOwnProperty('x') returns true because x is an own property of point. Object.hasOwn(point, 'hasOwnProperty') returns false because point has no own hasOwnProperty property. The method is found on Object.prototype through point's [[Prototype]] link.
Exercise
What does Object.getPrototypeOf(Object.prototype) return?
Show answer
null. Object.prototype is the end of the link for ordinary objects, so a lookup that reaches it and still fails returns undefined.