Sub topic 1
What Happens When You Touch an Object: Internal Methods
Every time your code reads a property, assigns a value, or calls a function, the JavaScript engine turns that syntax into a call to an internal method. Internal methods are operations defined in the ECMAScript specification. You cannot call them directly, but they decide how every object behaves. A Proxy is an object that lets you supply your own versions of these methods, so learning them first makes the rest of this tutorial easier to follow.
Prerequisites
This chapter assumes you are comfortable with:
- JavaScript objects, properties, and property descriptors (
writable,enumerable,configurable, getters and setters) - Prototypes and how property lookup walks the prototype chain
- Functions,
thisbinding, and constructors called withnew - ES6 syntax: arrow functions, classes, template literals, destructuring
- Symbols at a basic level
The Essential Internal Methods
The specification writes internal methods in double square brackets, such as [[Get]]. Every object has the eleven essential ones below. Function objects add two more.
| Internal method | Triggered by (examples) |
|---|---|
| `[[Get]]` | `obj.x`, `obj[key]` |
| `[[Set]]` | `obj.x = 1` |
| `[[HasProperty]]` | `'x' in obj` |
| `[[Delete]]` | `delete obj.x` |
| `[[OwnPropertyKeys]]` | `Object.keys(obj)`, `for...in`, `Object.getOwnPropertySymbols(obj)` |
| `[[GetOwnProperty]]` | `Object.getOwnPropertyDescriptor(obj, 'x')` |
| `[[DefineOwnProperty]]` | `Object.defineProperty(obj, 'x', desc)` |
| `[[GetPrototypeOf]]` | `Object.getPrototypeOf(obj)`, `instanceof` |
| `[[SetPrototypeOf]]` | `Object.setPrototypeOf(obj, proto)` |
| `[[IsExtensible]]` | `Object.isExtensible(obj)` |
| `[[PreventExtensions]]` | `Object.preventExtensions(obj)`, `Object.freeze(obj)` |
| `[[Call]]` (functions only) | `fn()` |
| `[[Construct]]` (constructors only) | `new Fn()` |
One piece of syntax often triggers several internal methods. Object.keys(obj), for example, calls [[OwnPropertyKeys]] to get the key list. It then calls [[GetOwnProperty]] on each key to check whether that key is enumerable.
Ordinary Objects Versus Exotic Objects
An ordinary object uses the default algorithm the specification gives for each internal method. Plain objects made with {} and instances of your own classes are ordinary. An exotic object replaces at least one of those algorithms. Arrays are exotic because their [[DefineOwnProperty]] updates length. A Proxy is also exotic: each of its internal methods hands the operation to your code if you provided some, and to the wrapped object if you did not.
Walking Through `[[Get]]`
The ordinary [[Get]] takes two inputs. The first is the property key. The second is the receiver, which is the object the lookup started on. The receiver matters when the lookup finds a getter higher up the prototype chain.
The code below shows the receiver at work. The getter lives on base, but it reads _name from child, because child is the object where the lookup began.
const base = {
_name: 'base',
get name() {
return `I am ${this._name}`;
}
};
const child = Object.create(base);
child._name = 'child';
// child has no own 'name', so [[Get]] moves to base.
// It finds the getter there and calls it with this = child (the receiver).
console.log(child.name); // "I am child"
// The same lookup, written so the receiver is visible:
const desc = Object.getOwnPropertyDescriptor(base, 'name');
console.log(desc.get.call(child)); // "I am child"[[Set]] follows a similar path. It looks for a setter or a read-only property along the chain. If it finds neither, it creates or updates an own data property on the receiver, not on the object where the search stopped.
Exercise
Which internal methods run when the engine evaluates 'x' in obj and then delete obj.x? If obj has no own x but its prototype does, which object does 'x' in obj end up checking?
Show answer
'x' in obj calls [[HasProperty]], and delete obj.x calls [[Delete]]. The ordinary [[HasProperty]] checks obj's own properties first, then calls [[HasProperty]] on the prototype, which finds x and returns true. [[Delete]] only removes own properties, so delete obj.x returns true and leaves the prototype's x in place.
Exercise
In the base/child example, what does base.name print, and why?
Show answer
It prints "I am base". The lookup starts on base, so base is the receiver, and the getter runs with this = base.