Concise Method Binding

Bergi a.d.bergi at web.de
Wed Nov 11 16:52:33 UTC 2015


Andrea Giammarchi schrieb:
> Just my thoughts, I wouldn't put any self-bound thing in the class and
> rather improve that `::` proposal so that whenever you `obj::method` it
> creates a uniquely bound callback so that `obj::method === obj::method`

This was considered: 
https://github.com/zenparsing/es-function-bind/issues/17 - it's quite 
unexpected that an operator would return the same result every time, and 
there are security considerations as well. Also it would be quite 
complicated to spec - how and where did you store the memoisation. Feel 
free to join the discussion!

Using the `::` operator in the class declaration itself makes sense to 
me. It conveys "this method will always be bound" very effectively, and 
you wouldn't even need special syntax to access it.
```js
class Xample {
   ::myListener(…) {…}
}
```
should desugar to
```js
class Xample {
   // a getter on the prototype
   get myListener() {
     // with per-instance memoisation
     return this.myListener = (…) => {
       // that returns a bound method
       …
     };
   }
}
```
It might be equivalently done via a custom decorator of course:
```js
class Xample {
   @autobind
   myListener(…) {…}
}
```

Regards,
  Bergi


More information about the es-discuss mailing list