How to fix the `class` keyword

Brendan Eich brendan at mozilla.org
Thu Mar 5 04:17:45 UTC 2015


Matthew Robb wrote:
> On the issue of calling class constructors, I would AT LEAST have 
> preferred implicit new on all calls to class constructors. Sure you 
> might get extra allocation weight but the way it stands now seems like 
> it could only lead to errors in people's assumptions...

An error caught by a throw is better than one that escapes as a 
silent-but-deadly (never mind allocation weight) difference in runtime 
semantics.

In case it helps, the idea mooted for ES7 is that you'd add a "call 
handler" to the class for when it is invoked without `new`:

   class Point2D {
     constructor(x, y) { this.x = x, this.y = y; }
     [Symbol.call](x, y) { return new this.constructor(x, y); }
     ...
   }

I used `this.constructor`, but of course using `Point2D` directly is 
possible. In that case, subclasses would have to override the 
`[Symbol.call]` method, which seems undesirable and easily avoided as 
shown above.

Bottom line, we don't want an implicit call handler in ES6. We need to 
get this right in ES7, and failing hard for now is the only way to be 
future-proof.

/be


More information about the es-discuss mailing list