On Sun, Jun 12, 2011 at 2:17 PM, Brendan Eich <bren...@mozilla.com> wrote:

> But to use the prototypal pattern, you would want something that has been
> discussed a bit: class elements (class body elements) defining
> once-per-class-evaluation function, let, and const bindings not as
> properties, but as lexical bindings in the scope of the prototype methods
> and accessors.
>

I worry about treating a class body like a block scope and allowing variable
declarations inside it. The syntax is really close to declaring
*properties* inside
the class (which is mostly what classes are for) and I worry people will get
confused.

More so, if we allow that, it means a class body becomes "block-like". It'll
be a lexical scope and you'll be able to declare variables in it. At that
point, I fear people will start expecting *any* statement to work inside a
class body. Once we open that can of worms, it gets really hard to find open
syntactic space for the stuff we care about: being able to tersely declare
class, prototype, and instance members.

As much as possible, I'd like to have a class body be just a collection of
property definitions. I find that simpler to understand and less likely to
cause us to paint ourselves into some syntactic corner.

I've raised the issue among a couple of people and threads (although
> possibly not here on es-discuss) of whether the class syntax should rather
> avoid using declaration syntax to bind prototype properties. Declarations
> otherwise create lexical bindings.
>

Why not just declare them outside of the class?


> class Monster {
>  ...
>  function log(...rest) { /*...*/}
>
>  attack(target) {
>    log('The monster attacks ' + target);
>  }
> }
>

If it was me, I'd probably prefer to just do:

module DungeonCrawl {
  export class Monster {
    attack(target) {
      log('The monster attacks ' + target);
    }
  }

  function log(...rest) { /*...*/}
}

To me, modules are a nice clean way to say "here's what I expose, and here's
what I want to use just for myself."


> class Monster {
>  ...
>  function log(...rest) { /*...*/}
>
>  public log = log;
>
>  attack(target) {
>    log('The monster attacks ' + target);
>  }
> }
>

Oh man, that's confusing looking.


> Another avenue: even without a change to enable declarations in class
> bodies to bind once-per-class lexical bindings in the scope of the prototype
> methods, another alternative comes to mind: private prototype methods. With
> private name objects, these could be true properties of the prototype but
> not nameable outside of the class body's braced extent.
>

I really like this idea.

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

Reply via email to