Finding a "safety syntax" for classes

Russell Leggett russell.leggett at gmail.com
Tue Mar 20 13:49:23 PDT 2012


>
> I don't know if I would write:
>
>  class Foo extends Bar {
>    constructor (x) { ... }
>    method1 () {}
>    method2 () {}
>  }.prototype.{
>    commonNum: 0,
>    commonColl: []
>  }
>
> or
>
>  function Foo (x) extends Bar {
>    ...
>  }.prototype.{
>    method1 () {}
>    method2 () {}
>    commonNum: 0,
>    commonColl: []
>
>  }
>
> If it were me, I would question the use of putting either commonNum or
commonColl on the prototype. commonNum is not as big a deal, but commonColl
is just asking for trouble. Modifying the commonColl array from an instance
will affect all other instances, but replacing it from an instance will
just mask it. If the first is the desired purpose, it would be better
places outside the class and captured in a closure variable. If the latter
is desired, it would be better to just put it inside the constructor
function. Avoiding non-methods steers us clear of controversy and still
gets us the 80%.

As for the rest,

    function Foo() extends Bar {...}

can easily be written as

    class Foo extends Bar {
        constructor(){...}
    }

so if it came down to only one I would take class, because I think it hits
a bigger sweet spot. I'm not opposed to both, though, and have written as
much in other proposals.

- Russ
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20120320/04b47f42/attachment.html>


More information about the es-discuss mailing list