Concise Method Binding

Bergi a.d.bergi at web.de
Tue Nov 10 02:39:21 UTC 2015


Jordan Harband schrieb:

 > JD Isaacks <jd at jisaacks.com> wrote:
 >
 >> class Foo {
 >>    bar = () => {
 >>      // bound
 >>    }
 >>    buz() {
 >>      // unbound
 >>    }
 >> }
> In your first example, the arrow function's "this" would be the "this" of
> the context the "class" is defined in - it wouldn't be bound at all.

No, the methods defined with "=" are automatically moved into the 
constructor, so that they are created once for each new instance. `this` 
in the arrow function would in fact be bound to the instance.

It's indeed super-confusing to "declare" such methods right inside the 
`class` though, that's why I'm really opposing that proposal 
(https://github.com/jeffmo/es-class-static-properties-and-fields).

Greetings,
  Bergi


More information about the es-discuss mailing list