Closed bathos closed 7 years ago
A lot of us (including me) would be very happy if we could use :
for initializing property declarations. Unfortunately, this is "reserved" for systems like TypeScript. which use the exact same syntax for type declarations. This is sort of codified in the fifth bullet point of Forbidden Extensions:
The Syntactic Grammar must not be extended in any manner that allows the token
:
to immediately follow source text that matches the BindingIdentifier nonterminal symbol.
Wow, thanks for the answer. I am pretty surprised to learn this is a codified constraint given how (relatively) small and young those non-standard extensions are. (It's a little disheartening to entertain cynical thoughts about what this might suggest about priorities.) Closing since it’s a lost cause.
Right now, class syntax mirrors object literal syntax in terms of defining methods and accessors, which seems really elegant to me since it makes things intuitive and predictable, and also hints at what a class definition really is:
Syntactically the main differences are that object literals require commas, that class bodies permit semicolons, and that object literals permit shorthand properties. That the
static
modifier is available only in class syntax makes sense, since it is only applicable there.I’ve been wondering why this is not also the approach being taken with non-method, non-accessor properties of the prototype, constructor (and now instance). That is, is there a reason to prefer this:
over this?:
Maybe there are grammar issues I’m missing, but these are the advantages I think it has:
=
seems to imply that the class body is a scope and that the property is a variable in that scope — but the class body is not a scope, and these are property definitions