Skip to content

Instantly share code, notes, and snippets.

@rauschma
Created November 17, 2011 19:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rauschma/1374226 to your computer and use it in GitHub Desktop.
Save rauschma/1374226 to your computer and use it in GitHub Desktop.
A synthesis of ECMAScript.next class proposals

A minimal class proposal

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.

  1. 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
  2. Introduce an operator classof that returns the exemplar that created an instance.

  3. Keep <| as currently proposed (including its name), as an elegant version of Object.create(). Rationale: To be used by expert programmers only.

  4. 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.

  5. Optional: name object support via [] or @ in object initializers.

     class C {
         // private method
         @incAge() {
             this.@age++;
         }
     }
    
  6. 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) {
                 // ...
             }
         }
     }
    
  7. 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
@Raynos
Copy link

Raynos commented Nov 17, 2011

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

@rauschma
Copy link
Author

@Raynos: I’ve edited for clarity.

@Raynos
Copy link

Raynos commented Nov 17, 2011

How do you solve the the missing constructor problem? Do you use allen's proposed [[ctor]] property?

@rauschma
Copy link
Author

@Raynos: It’s much less of a problem, because you don’t have an operator whose operand is an object. Options: use an empty function, use an empty function that invokes super.constructor(), throw an error.

@Raynos
Copy link

Raynos commented Nov 17, 2011

I think using an empty function that invokes super.constructor is the most sensible default.

@Raynos
Copy link

Raynos commented Mar 16, 2012

What is a static property? Is it just a read only property?

@rauschma
Copy link
Author

Edited for clarity...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment