2

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:

  1. 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'(…)

  1. 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?
SanBen
  • 2,581
  • 26
  • 35
Drylozav
  • 183
  • 1
  • 9

1 Answers1

5

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;
}
Gerrit0
  • 7,955
  • 3
  • 25
  • 32
  • But at what momen this type conversion occurs ? I mean from string to boolean. I can't find any explanation in specification. – Drylozav Nov 28 '16 at 20:43
  • 1
    @Drylozav It appears to happen internally, after you return from the set handler. – Gerrit0 Nov 28 '16 at 20:45