On Thu, Sep 29, 2011 at 4:22 PM, Erik Arvidsson <erik.arvids...@gmail.com>wrote:

> However, it seems like all the issues we have seen are due to us
> trying to solve issues that already exist today with prototype based
> "classes". These involve (but are not limited to):
>
> 1. Don't let uninitialized objects escape
> 2. Ensure the shape of the instance
> 3. Initialization of instance properties
> 4. Allow const classes
> 5. Allow const properties
> 6. Play well with future type guards
>

I was tinkering with some syntax ideas last night and had the same
revelation. It feels like we've over-constrained ourselves. In particular,
if you're willing to discard 2 and 6 (basically not worry about a
declarative form for instance properties) I think it gets a lot easier.

If we throw in the sections stuff that we discussed a while back, that could
give you something like:

class Monster {
  constructor(this.name, this.health) {}

  attack(target) {
    log(Monster.attackMessage);
  }

  get isAlive() {
    return this.health > 0;
  }

  set health(value) {
    if (value < 0) throw new Error('Health must be non-negative.')
    this.health = value
  }

  let numAttacks = 0;

class:
  const attackMessage = 'The monster hits you!';
}

This means:

- A class body contains only definitions. By default those definitions
become properties on the prototype. You can change that by adding a section.
Everything after class: goes on the constructor.

- Instance properties are not declared explicitly. But allowing "this." in
the constructor argument list gives you a nice notation for automatically
initializing them from arguments.

- Here I'm using "let" to declare a mutable data property in the class body.
We could use whatever. It's a pretty open field since the body doesn't
contain arbitrary statements.

This is pretty close to how both Ruby and Python classes behave and I
> think we don't have to solve the problems raised above for ES.next.
>

I like this. Maybe in pursuit of perfect we've lost sight of good?

- bob
_______________________________________________
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to