Class method shorthand return

T.J. Crowder tj.crowder at farsightsoftware.com
Fri May 5 17:58:18 UTC 2017


On Fri, May 5, 2017 at 6:49 PM, Brian Ninni <ninni.brian at gmail.com> wrote:

> I think it might be problematic, since (if it works the same way as arrow
> functions currently do) 'this' will refer to the context in which the class
> was defined, and not to the instance that the function is attached to.
>

Not if we make it consistent with [public class fields](
https://tc39.github.io/proposal-class-public-fields/) (and we'd want to).
Within the `class` structure, `this` refers to the instance (in that
proposal at least). Which is consistent with method syntax and Serghei's
suggestion here.

This is in significant use in the React community already, via transpiling,
e.g. this works:

```js
class Widget extends React.Component {
    handleClick = _ => {
        this.setState(state => ({counter: state.counter + 1}));
    };
    render() {
        return <div onClick={this.handleClick}>{this.state.counter}</div>;
    }
}
```

The public class fields stuff also weakens the case for Serghei's concise
method syntax, since you can do the above.

-- T.J. Crowder
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20170505/fa67503c/attachment.html>


More information about the es-discuss mailing list