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:
- Why do I get an error if I set the property status, on the last line, to 0 or ""(empty string)?
Uncaught TypeError: 'set' on proxy: trap returned falsish for property 'status'(…)
- In the specification it says that I should return a boolean value. But in my case, in the set() method I do not return any boolean value. In that case why does this code work?