Instance bound class methods
Sebastian Markbåge
sebastian at calyptus.eu
Fri Jul 17 23:17:35 UTC 2015
Brendan, "the pay for what you ask for" model isn't very efficient when you keep asking for the same thing multiple times (like React is doing in its frequently executed render function). We want it to at least be memoized (presumably in a slot on the instance). Lazily creating it is an unobservable implementation detail.
> On Jul 13, 2015, at 8:02 AM, Brendan Eich <brendan at mozilla.org> wrote:
>
> You're counting on the property assignment being moved into the constructor, where `this` is bound. In a class body in ES6 (without property assignment extension), especially in the top level where method defintiions go, `this` isn't obviously bound to the newly constructed instance.
>
> Ok, that's kind of a nit, or an observation. No worries.
>
> Bigger question is what we want: method per instance allocation cost, which what you did imposes? Or something that can be more efficiently implemented, such as what Strong/SoundScript proposes (last I looked). The latter is what Java and C++ do. Then the trick is enabling first-class function extraction from method, which is a pay-for-what-you-ask-for alternative to allocation per method per constructed instance.
>
> /be
>
> Matthew Robb wrote:
>> Are there any proposals or any discussions around solving the problem of instance bound class methods with some sugar?
>>
>> There are examples of people doing things like this: https://github.com/reactjs/react-future/blob/master/01%20-%20Core/01%20-%20Classes.js#L31
>>
>> My proposal would be to extend method shorthand syntax to support the arrow:
>>
>> ```
>> class A extends React.Component {
>> handleClick(event)=> {
>> ...
>> }
>> }
>> ```
>> Which would be sugar for:
>> ```
>> class A extends React.Component {
>> handleClick = (event)=> {
>> ...
>> }
>> }
>> ```
>> - Matthew Robb
>> _______________________________________________
>> es-discuss mailing list
>> es-discuss at mozilla.org
>> https://mail.mozilla.org/listinfo/es-discuss
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
More information about the es-discuss
mailing list