dynamic class idiom

Brendan Eich brendan at mozilla.org
Tue Aug 26 16:34:22 PDT 2008


On Aug 26, 2008, at 4:16 PM, Dave Herman wrote:

>> Does not say class, seems to save only a few chars (you
>> over-parenthesize new ;-). Wait for demand?
>
> It's not about saving chars so much as introducing a new (lightweight)
> semantic concept: an object initializer that is a block rather than a
> table mapping names to expressions. Not saying `class' is the point.

But you'd say class if you needed to define the operand to new for  
other uses, or for some other reason motivating against this  
"lightweight" form. So the concept seems to have two names when  
generalized slightly.

Specializations can have special names, don't get me wrong. But  
object is way overloaded.


> I over-parenthesized because I wasn't sure how it would parse if you
> said e.g.
>
>      new class() { ... }.foo

That should be unambiguous, and work as this does in ES1-3:

     new MyClass(arg).foo

The grammar goes to some lengths to group (new MyClass(arg)).


>>> 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.
>
> Right, but that means that existing code isn't using this form, so you
> could steal this special case; then the argument to `new' would  
> have to
> be an expression that didn't start with '{', just like expression
> statements. It's backwards-incompatible but since the current syntax
> only leads to a useless error, I doubt real code is using it.

We consider such guaranteed errors to be unused, and call changes to  
claim the syntax backward-compatbile. At least we did in the ES4 WG.  
ES3 chapter 16 allows implementations to extend in this way, too.

/be



More information about the Es-discuss mailing list