May the defineProperty method of a proxy handler throw a TypeError?

David Bruant david.bruant at labri.fr
Fri Jun 17 16:20:23 PDT 2011


Le 18/06/2011 00:47, David Flanagan a écrit :
> On 6/17/11 3:05 PM, David Bruant wrote:
>> What about adding a "throw" argument to the defineProperty trap (and any
>> trap for which the equivalent internal method has a "throw" argument) ?
>> The engine would set the boolean value correctly depending on strict
>> mode or any relevant parameter. Trap writers can adapt their behavior
>> based on this boolean.
>>
>> David
> http://wiki.ecmascript.org/doku.php?id=harmony:proxies_semantics
> already defines appropriate Reject behavior (return false or throw)
> for the [[Put]] and [[Delete]] operations based on the return value of
> the put() and delete() traps.  [[DefineOwnProperty]] is the only one
> that does not allow it.
>
> Given that the fixed properties proposal is adding a return value to
> defineProperty, perhaps returning false would be a suitable way to
> reject a property definition.
I would see this as a weird semantic collision. If I return undefined,
should it be considered as a falsy value or an inappropriate property
descriptor? If I return {} or [], is it a truthy value or an invalid
property descriptor?
I would be much more in favor of traps closely following internal
methods signature, meaning add "Throw" arguments and let trap writers
decide of what they want to do with it. Following internal method
signature sounds a safer way to get around what you need to do.
Currently, the "Throw" argument seems bound to strictness (needs to be
proven, i do not know ES5 by heart), but who knows what it's going to be
used for in the future (what other contextual information will influence
its value)?

----
delete(a,Throw){
  if(Throw)
    throw new Error();
  else
    return false;
}

// VS

delete(a){
  return false;
}
----
I think it's more clear for the reader that an error can be thrown from
the former case. The latter case requires implicit knowledge and may be
harder to debug. Of course, this is very subjective as I have never had
the occasion to debug such a code.

David


More information about the es-discuss mailing list