dynamic class idiom

Brendan Eich brendan at mozilla.org
Tue Aug 26 16:10:20 PDT 2008

On Aug 26, 2008, at 2:49 PM, Dave Herman wrote:

> I noticed a little idiom supported by classes-as-values:
>      (new (class() { ... }))
> This essentially builds an object whose properties can be computed  
> in an
> arbitrary statement context-- with loops or what have you. You might
> call this a "dynamic singleton" pattern; a throwaway factory. It's  
> not a
> global singleton like in Java; it's just an ordinary expression so it
> might be called by a function or whatever.

Probably everyone involved favors first-class classes -- first-class  
meaning classes as values (not only as named defining forms),  
nestable. I hoped so in reply to Peter yesterday.

> Worth a little sugar? E.g.:
>      object { ... } ~=~ (new (class() { ... }))

Does not say class, seems to save only a few chars (you over- 
parenthesize new ;-). Wait for demand?

> or maybe to conserve keywords (a bit backwards-incompatible):
>      new { ... } ~=~ (new (class() { ... }))

This is not incompatible at all, since an object initialiser has no  
[[Construct]] in ES3, so cannot be the operand of new.


More information about the Es-discuss mailing list