Emmanuel Akinfulubi

JavaScriptFundamental (ES6), part 2

Log every property access with a Proxy in JavaScript

60 An access log, and the reads you did not know you made

fair

Trap get, set, has, deleteProperty and ownKeys to record every operation on an object. Then run spread and JSON.stringify over it and account for every line in the log, including the ones you did not write.

Stuck? Three hints, smallest first
  1. 1. Record the operation and the key, then forward through the matching Reflect method.
  2. 2. Spread is not one operation: it asks for the keys first, then reads each one.
  3. 3. JSON.stringify asks the object for something before it asks for any of your data.

The answer

const log = [];
const watch = (target) => new Proxy(target, {
  get(t, k, r) { log.push('get ' + String(k)); return Reflect.get(t, k, r); },
  set(t, k, v, r) { log.push('set ' + String(k)); return Reflect.set(t, k, v, r); },
  has(t, k) { log.push('has ' + String(k)); return Reflect.has(t, k); },
  deleteProperty(t, k) { log.push('delete ' + String(k)); return Reflect.deleteProperty(t, k); },
  ownKeys(t) { log.push('ownKeys'); return Reflect.ownKeys(t); },
});

const trip = watch({ stop: 'Ojota', fare: 400 });

trip.stop;
trip.fare = 450;
const asked = 'fare' in trip;
const gone = delete trip.stop;
const afterFour = log.slice();

log.length = 0;
const keys = Object.keys(trip);
const afterKeys = log.slice();

log.length = 0;
const copy = { ...trip };
const afterSpread = log.slice();

log.length = 0;
const json = JSON.stringify(trip);
const afterJson = log.slice();

console.log('four operations:', afterFour);
console.log('Object.keys    :', afterKeys);
console.log('spread         :', afterSpread);
console.log('stringify      :', afterJson, '->', json);

Output


      
8 checks that prove this answer

You do not have to read these. They are part of the answer panel above, so pressing Run there executes all 8 of them and prints ok for each. That output is the proof, and the build fails if a single check fails.

8 checks

test('the four operations you wrote are logged in order', afterFour, ['get stop', 'set fare', 'has fare', 'delete stop']);
test('and they did what they normally do', [asked, gone, keys], [true, true, ['fare']]);
test('Object.keys is one ownKeys, with no reads at all', afterKeys, ['ownKeys']);
test('spread is two operations: ask for the keys, then read each one', afterSpread, ['ownKeys', 'get fare']);
test('so the copy is a plain object with the same data', copy, { fare: 450 });
test('JSON.stringify asks for toJSON before it asks for anything of yours', afterJson[0], 'get toJSON');
test('then it does the same keys-then-read pass', afterJson.slice(1), ['ownKeys', 'get fare']);
test('and the result is what you would expect from the data', json, '{"fare":450}');

The log is the lesson: high-level syntax decomposes into low-level operations, and a proxy is the only way to watch it happen. Spread is not a copy primitive, it is [[OwnPropertyKeys]] followed by a [[Get]] per key — which is why spreading an object with a getter runs the getter, and why spreading a proxy with an expensive get trap is expensive per key rather than once.

JSON.stringify is the one that surprises people. Its first act is get toJSON, on your object, before it has looked at a single key. If a get trap answers that with anything callable, that function decides the entire output. It is also why a logging proxy over a domain object shows a toJSON read that appears nowhere in the codebase.

Object.keys logs one ownKeys and no reads, because it wants names, not values. It does also perform a [[GetOwnProperty]] per key to check enumerability — untrapped here, so it goes straight to the target and leaves no line in the log. Exercise 61 is what happens when ownKeys and that check disagree.

A note on String(k) in each trap: a key can be a symbol, and string-concatenating a symbol throws. Logging 'get ' + k would crash the moment anything read Symbol.iterator or Symbol.toPrimitive off the object — which spread and await both do.

Newsletter

New writing, when it lands.

Occasional notes on shipping software with AI agents. No schedule, no filler, unsubscribe in one click.