Sub topic 2
Every Object Has a Hidden Link: [[Prototype]]
This chapter assumes you can write object literals, call methods, and run code in Node.js or a browser console. It covers one idea: every JavaScript object carries a hidden reference to another object, or to null. That reference is how inheritance works in JavaScript. Later chapters show how lookup follows it and how to set it. Here the goal is to see that the link exists, find out where it points for common objects, and separate it from a similarly named property that causes a lot of confusion.
What the Link Is
The ECMAScript specification gives every object an internal slot called [[Prototype]]. An internal slot is a piece of engine-managed state that your code cannot reach with normal property syntax. The double square brackets are the spec's notation for these slots. They are not JavaScript syntax.
The value of [[Prototype]] is always one of two things:
- a reference to another object, called the object's prototype
null, meaning the object has no prototype
When you read a property the object does not have itself, the engine follows this link and looks on the prototype instead. The next chapter covers that walk in detail. What matters now is that the prototype is referenced, not copied. The object stores a pointer to its prototype, so nothing from the prototype is duplicated into the object.
Seeing the Link
The link is not a property, so Object.keys, for...in over own keys, and JSON.stringify never show it. You inspect it with Object.getPrototypeOf. A later chapter covers that function and its relatives in full. Browser DevTools also show it as a [[Prototype]] entry when you expand an object.
const user = { name: 'Ada' };
console.log(Object.keys(user)); // ['name']
console.log(JSON.stringify(user)); // '{"name":"Ada"}'
console.log(Object.getPrototypeOf(user) === Object.prototype); // true
// toString is not on user, but the call still works
console.log(user.toString()); // "[object Object]"
console.log(Object.hasOwn(user, 'toString')); // falseuser has one own property, name. Its [[Prototype]] points at Object.prototype, which is where toString lives. Every object literal gets this link automatically.
Where the Link Points by Default
Each kind of built-in value gets a matching prototype when it is created. Those prototypes are ordinary objects with their own [[Prototype]] links.
const list = [1, 2, 3];
const fn = function () {};
const when = new Date(0);
const pattern = /ab+c/;
console.log(Object.getPrototypeOf(list) === Array.prototype); // true
console.log(Object.getPrototypeOf(fn) === Function.prototype); // true
console.log(Object.getPrototypeOf(when) === Date.prototype); // true
console.log(Object.getPrototypeOf(pattern) === RegExp.prototype); // true
// The built-in prototypes link onward to Object.prototype
console.log(Object.getPrototypeOf(Array.prototype) === Object.prototype); // true
console.log(Object.getPrototypeOf(Function.prototype) === Object.prototype); // true
// Object.prototype is where the links end
console.log(Object.getPrototypeOf(Object.prototype)); // null| Value created with | Its [[Prototype]] | That object's [[Prototype]] |
|---|---|---|
| `{}` | `Object.prototype` | `null` |
| `[]` | `Array.prototype` | `Object.prototype` |
| `function () {}` or `() => {}` | `Function.prototype` | `Object.prototype` |
| `new Date()` | `Date.prototype` | `Object.prototype` |
| `/re/` | `RegExp.prototype` | `Object.prototype` |
| `Object.create(null)` | `null` | none |
Primitives Borrow a Link
Primitives such as strings and numbers are not objects and have no internal slots. Calling 'abc'.toUpperCase() still works because the engine temporarily wraps the primitive in an object whose [[Prototype]] is String.prototype. Since ES2015, Object.getPrototypeOf performs the same conversion. The exceptions are null and undefined, which have no wrapper type.
console.log(Object.getPrototypeOf('abc') === String.prototype); // true
console.log(Object.getPrototypeOf(42) === Number.prototype); // true
try {
Object.getPrototypeOf(null);
} catch (err) {
console.log(err instanceof TypeError); // true
}The Link Is Live
Because the link is a reference, changes to the prototype object show up right away in every object that links to it, including objects created before the change. The example uses Object.create, which makes a new object with a chosen [[Prototype]]. A later chapter covers it properly.
const base = {
greet() {
return 'hello';
}
};
const child = Object.create(base);
console.log(child.greet()); // "hello"
// Add a method to base after child already exists
base.wave = function () {
return 'waving';
};
console.log(child.wave()); // "waving"
console.log(Object.keys(child)); // []child has no own properties at all. Every method it can use comes through its link to base.
[[Prototype]] Versus a Function's prototype Property
This is the most common source of confusion in the topic. Functions have an ordinary, visible property named prototype. It has a similar name to the internal [[Prototype]] slot but does something different.
[[Prototype]]is the hidden link that every object has, functions included. It controls what that object inherits.fn.prototypeis a normal property found on most non-arrow functions. It holds the object that will become the[[Prototype]]of instances created withnew fn(). It has no effect on what the function itself inherits.
function Widget() {}
// The function's own hidden link: what Widget itself inherits from
console.log(Object.getPrototypeOf(Widget) === Function.prototype); // true
// The visible property: a separate object meant for future instances
console.log(typeof Widget.prototype); // "object"
console.log(Object.getPrototypeOf(Widget) === Widget.prototype); // false
// Arrow functions cannot be used with new, so they have no prototype property
const arrow = () => {};
console.log(arrow.prototype); // undefined
console.log(Object.getPrototypeOf(arrow) === Function.prototype); // trueCommon Mistakes
| Mistake | What actually happens |
|---|---|
| Expecting `obj.prototype` to be the object's prototype | Plain objects have no `prototype` property, so `obj.prototype` is `undefined`. Use `Object.getPrototypeOf(obj)`. |
| Assuming inherited methods are copied into the object | Methods are reached through the link. `Object.hasOwn` returns `false` for them. |
| Expecting `Object.keys` or `JSON.stringify` to include inherited data | Both see only own properties. |
| Calling `Object.getPrototypeOf(undefined)` | It throws a `TypeError`. Guard the value first. |
| Assuming every object eventually reaches `Object.prototype` | Objects made with `Object.create(null)` have no prototype at all. |
Exercises
Exercise
Without running it, predict each line's output: const arr = []; console.log(Object.getPrototypeOf(arr) === Array.prototype); console.log(Object.getPrototypeOf(Object.getPrototypeOf(arr)) === Object.prototype); console.log(arr.prototype);
Show answer
true, true, undefined. An array's [[Prototype]] is Array.prototype, whose [[Prototype]] is Object.prototype. Arrays are not functions, so they have no ordinary prototype property.
Exercise
Given function Car() {}, is Object.getPrototypeOf(Car) === Car.prototype true or false? Explain which object each side refers to.
Show answer
False. Object.getPrototypeOf(Car) is Car's own hidden link, which points to Function.prototype because Car is a function. Car.prototype is an ordinary property holding a separate object that will become the [[Prototype]] of objects created with new Car().
Exercise
You create const a = Object.create(proto) and later run proto.ready = true. What does a.ready return, and what does Object.hasOwn(a, 'ready') return?
Show answer
a.ready is true, because a reaches proto through its live [[Prototype]] reference. Object.hasOwn(a, 'ready') is false, because the property lives on proto and was never copied to a.