Type of property names, as seen by proxy traps

Brendan Eich brendan at mozilla.com
Thu Jul 7 08:58:58 PDT 2011


On Jul 7, 2011, at 8:32 AM, Andreas Rossberg wrote:

> On 7 July 2011 16:12, David Bruant <david.bruant at labri.fr> wrote:
>>>> Derived traps as showed are written in JS for expository purposes.
>>>> Engines
>>>> will be free to optimize as they wish internally as long as the observed
>>>> behavior is the same.
>>> 
>>> True, but optimizing that actually is more tricky than you might
>>> think, since in general it would change the semantics if an engine
>>> decided to call toString only once. It has to make sure that none of
>>> the names are objects, or at least none of their toString methods was
>>> modified and they are all free of side effects.
>> 
>> Interesting.
>> However, I'm not sure side-effects are a problem.
>> -----
>> var o = {a:1, toString:function(){o.b = 12; return 'a'; }};
>> console.log(o[o], o.b); // 1, 12 on Firefox 5
>> -----
>> Here, o[o] triggers a side effect and that sound like the normal behavior.
> 
> I'm not sure I understand what your example is intended to show. But
> consider this:
> 
> var i = 0
> var o = {toString: function() { ++i; return "a" }
> var p = Proxy.create({getOwnPropertyNames: function() { return [o] }, ...})
> var k = Object.keys(p)
> // What's the value of i now?

Fresh tracemonkey tip js shell:

js> var i = 0
js> var o = {toString: function() { ++i; return "a" }}
js> var p = Proxy.create({getOwnPropertyNames: function() { return [o] },
                      getOwnPropertyDescriptor: function() { return {value:42} }})
js> var k = Object.keys(p)
js> i
1


Where would there be a double-conversion?

/be



More information about the es-discuss mailing list