Drylozav Drylozav - 1 year ago 198
Javascript Question

handlet.set in ES6 Proxy return value

In this code

function report(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:

1) Why if I set the value of the property status on the last line in 0 or ""(empty string) I get an error

Uncaught TypeError: 'set' on proxy: trap returned falsish for property 'status'(…)

2) In the specification it is said 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 this code work ?

Answer Source

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;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download