Skip to content

OutputNot attempted

A logging proxy

const user = { name: 'Dival', age: 25 };
const userProxy = new Proxy(user, {
get(target, prop) {
console.log('get', prop);
return target[prop];
},
});
userProxy.name = 'Minal';
console.log(userProxy.name, user.name);

Spaces and quote style don’t matter · write a thrown error as TypeError

Ctrl Enter
Show answer & explanation

The expected output comes from actually running the snippet; opening this shows it next to the explanation.

Only reads are trapped. The write goes straight through to the target (there's no set trap), so user.name changes too. Reading userProxy.name logs "get name" first, while console.log is still evaluating its arguments.