Could you include some examples of *calling* functions defined this way? The most obvious way uses `Function#call` and would be terribly awkward. Perhaps I'm just overlooking some ES6 feature which makes passing a specific `this` value easy?
It seems curious that you are not using methods instead of functions. --scott On Wed, Jun 17, 2015 at 2:41 AM, Jussi Kalliokoski < [email protected]> wrote: > It's probably a bit early for this, but I figured I'd put it out there (I > already proposed this as a tangent in the function bind syntax thread). > This syntax proposal is purely about convenience and subjective > expressiveness (like any feature addition to a Turing complete language). > > As I've been building Trine, I noticed that the "`this` as data" pattern > is extremely powerful and expressive, however the code in the functions > doesn't convey the intent very clearly. For example: > > function add (b) { return this + b; } > function * map (fn) { for ( let item of this ) { yield item::fn(); } } > > vs. > > function add (a, b) { return a + b; } > function * map (iterator, fn) { for ( let item of iterator ) { yield > item::fn(); } } > > Also currently neither Flow or TypeScript support type annotating this. > There's discussion [1] [2] in both the projects for allowing `this` to be > specified as a parameter to allow annotating it, e.g. > > function add (this : number, b : number) : number { return this + b; } > > This leads to my current proposal, i.e. being able to make the first > parameter of the function an alias for `this` by using a special prefix > (&). This would not only allow aliasing `this`, but also destructuring and > default values (as well as type annotation in language extensions). > > The different forms and their desugarings: > > function add (&a, b) { > return a + b; > } > > // would desugar to > > function add (b) { > var a = this; > return a + b; > } > > > function multiplyTuple (&[a, b], multiplier) { > return [a * multiplier, b * multiplier]; > } > > // would desugar to > function multiplyTuple (multiplier) { > var [a, b] = this; > return [a * multiplier, b * multiplier]; > } > > > function multiplyPoints (&{ x1: x, y1: y }, { x2: x, y2: y }) { > return { x: x1 * x2, y: y1 * y2 }; > } > > // would desugar to > function multiplyPoints (_p2) { > var { x1: x, y1: y } = this; > var { x2: x, y2: y } = _p2; > return { x: x1 * x2, y: y1 * y2 }; > } > > > // allow passing the element for mocking in tests > function isQsaSupported (&dummyElement = document) { > return typeof dummyElement.querySelectorAll !== "undefined"; > } > > > This proposal would also be consistent with the type annotation proposals > for `this` mentioned earlier. > > WDYT? > > [1] https://github.com/facebook/flow/issues/452 > [2] https://github.com/Microsoft/TypeScript/issues/1985 > > _______________________________________________ > es-discuss mailing list > [email protected] > https://mail.mozilla.org/listinfo/es-discuss > >
_______________________________________________ es-discuss mailing list [email protected] https://mail.mozilla.org/listinfo/es-discuss

