My ECMAScript 7 wishlist

Brendan Eich brendan at mozilla.org
Fri Jun 6 15:53:21 PDT 2014


Nicholas C. Zakas wrote:
> It can be done with Proxy, but that kind of sucks because I always 
> need to go through the extra step of creating the Proxy for each 
> object and passing around the Proxy instead. To me, this is similar to 
> setting a property to be readonly in strict mode, except its writeonly 
> (or rather, write-first).

What about the code I showed, which shows a singleton being spliced high 
on many objects' prototype chains to handle the missing property throw?

js> var NoSuchProperty = Proxy({}, {
   has: function(target, name) { return true; },
   get: function(target, name, receiver) {
     if (name in Object.prototype) {
       return Reflect.get(Object.prototype, name, receiver);
     }
     throw new TypeError(name + " is not a defined property");
   }
});
js> var obj = Object.create(NoSuchProperty)
js> obj.foo = 42
42
js> obj.foo
42
js> obj.bar
/tmp/p.js:7:4 TypeError: bar is not a defined property

You could avoid Object.create by assigning to a Constructor.prototype, 
or hacking with __proto__, of course.

/be
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20140606/d70380e6/attachment.html>


More information about the es-discuss mailing list