In this code
function report(message) {
console.log(message);
}
function makeLoggable(target) {
return new Proxy(target, {
get(target, property) {
report(`Reading ${property}`);
const param = target;
return param[property];
},
set(target, property, value) {
report(`Writing value ${value} to ${property}`);
const param = target;
return param[property] = value;
},
});
}
let ninja = { name: 'Jack' };
ninja = makeLoggable(ninja);
console.assert(ninja.name === 'Jack', 'Our ninja Jack');
ninja.status = '';
I have two questions:
Uncaught TypeError: 'set' on proxy: trap returned falsish for property 'status'(…)
You are getting a TypeError because ""
is falsy. You would receive the same error if you tried to set the property value to 0
or false
.
As MDN states
In strict mode, a false return value from the set handler will throw a TypeError exception.
MDN is somewhat unclear here as it appears that any falsy value (not just false
) will cause the set handler to throw a TypeError.
Your code works for most cases because you are returning the result of the assignment. ninja.test = 'string'
will return string
which is truthy.
In order to fix this problem, simply change your set function to the following:
set(target, property, value) {
report(`Writing value ${value} to ${property}`);
const param = target;
param[property] = value;
return true;
}