Yehuda Katz wrote:
Yehuda Katz
(ph) 718.877.1325
On Tue, Oct 16, 2012 at 12:51 PM, Herby Vojčík <[email protected]
<mailto:[email protected]>> wrote:
This did not get to the list, reposting...
-------- Original Message --------
From: Herby Vojčík <[email protected] <mailto:[email protected]>>
Subject: Fail-fast on non-existing [[Get]]
Date: Fri, 12 Oct 2012 22:42:05 +0200
To: ECMAScript discussion <[email protected]
<mailto:[email protected]>>
Hello!
I have just looked the video on channel9 between Lars Bak and Anders
Hejlsberg, and it gave me one idea.
It seems people sometimes worry about (basically because of a typo) that
they [[Get]] a non-existing property but as it stands, it just returns
undefined and goes happily on.
I see two solutions:
- add something to the freeze, seal, preventExtensions family which
would set an object to the fail-fast [[Get]] state.
- use a Proxy in the proto chain to trap the unknown [[Get]] and
throw early
Using a proxy for your own objects to throw seems reasonable, but see
below for breakage to existing JS patterns. Adding it globally (via
Object.protoype.__proto__) would almost certainly break other JS code on
the page.
Consider this very common pattern:
obj.foo = obj.foo || bar;
I was too dense, again. By no means in Object.prototype.__proto__. I
meant "in the prototype chain for the class of object I want to throw on
unknown [[Get]]. IOW, under Object.prototype.__proto__.
(I put it in the P.S. down there...)
As a matter of practice, lots of JS code relies on the soft-fail
behavior, for good reason, and trying to change that behavior would not
likely result in happiness for consumers of the code that introduced
this change.
Sure.
Do you think this needs a solution at all? If yes, which option would
you favour?
Herby
P.S.: In the latter case, I see the possibility of putting
ClosedFormObject (or whatever name) constructor function which would
have a proxy for a .prototype just one step below Object, and let
classes that would want to throw on default-undefined-[[Get]] just
derive from that or its ancestors. Do you think this is minimal enough
so it can be put directly into language (if the solution does not have
some flaws which are not apparent to me)?
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss