Given that most things that matter at “class” level also matter at object level, I still feel that using object initializer notation for class declarations is the simplest solution. The following is a synthesis of some of the proposals out there.
-
Introduce Allen’s class operator as a declarative statement and an expression (similar to Jeremy’s and David’s proposals). IMHO, that’s best when it comes to ease of use for newbies.
class C { ... } class C extends B { ... } let C = class { ... }; let C = class extends B { ... };
The class body
{ ... }
is in object initializer syntax. Both class declaration and class expression produce a function (exemplar).
Options:- only allow methods inside the class body
- a class definition as syntactic sugar for an object exemplar
-
Introduce an operator
classof
that returns the exemplar that created an instance. -
Keep <| as currently proposed (including its name), as an elegant version of Object.create(). Rationale: To be used by expert programmers only.
-
Optional: support private names via
private { myname1, myname2 } // syntactic sugar for module name from "@name"; var myname1 = name.create(), myname2 = name.create();
Such a block can be put anywhere, including the inside of an object initializer.
-
Optional: name object support via [] or @ in object initializers.
class C { // private method @incAge() { this.@age++; } }
-
Optional: support for constructor properties.
static
is not the best of all names, but it is already a keyword.class C { static { #CONST_VALUE: 12345, anotherValue: "abc", mymethod(param1, param2) { // ... } } }
-
Optional (but I’d love to have this – the rationale is that that finishes what Object.create() started): ensure that the following operators work for both function exemplars and object exemplars:
- instanceof
- classof
- <|
- new
Does
class C {}
return a function exemplar or an object exemplar?I'd personally be very happy with the top 3, given that
class C
works without strange edge cases