Bound instance-function vending (was RE: Arrow binding)

Angus Croll anguscroll at gmail.com
Sun Apr 29 09:28:35 PDT 2012





On Apr 28, 2012, at 22:24, Axel Rauschmayer <axel at rauschma.de> wrote:

> The following two rules should be everything one has to know regarding callable entities, in ES.next:
> 1. Method: `this` is an implicit parameter => use a method definition.
> 2. Non-method function: `this` is not an implicit parameter => use an arrow function.
> 
> In this light, I’d rewrite Kevin’s code slightly, because, conceptually, `localStorage ` is a non-method function:
>  const localStorage = (obj) => {
> 
>    Object.extend(obj, {

If I understand correctly, this approach negates the need for a mixin function entirely - localStorage could just be an old style mixin hash. 

But either way this tosses out the benefits I get from functional mixins. By invoking the mixin instead of copying it I can a) customize it's behavior by passing it arguments b) have the mixin add "advice" (before, after, around) to functions of the target object.


> I think Angus’ most important point is this:
> 
>> Yes, people get confused by this binding; even though it is not hard to learn the rules, and I know them very well, I still trip over them sometimes. But unless we are going to introduce a "hella strict" mode that reverts all previous rules of |this| binding, yet another rule will just add to the morass.
> 
> It would be my hope that we can replace all previous rules with the two rules above. `apply` will mostly be replaced by the spread operator, `call` will mostly be replaced by calling a value. Some code such as `forEach` implementations that previously needed to use `call` to simulate lexical scoping don’t need a `thisValue` parameter for array functions.

I admire the vision but in reality the boundary between ES5 and ES6 will be messy and drawn out - there will be no clear cut switching point. Legacy code must live alongside the new vision. Without a new strict mode which banishes much of the old spec (and kills my functional mixins) I'm finding it hard not to picture chaos. (and how do you shim => anyway?)



More information about the es-discuss mailing list