[Idea] Bind operator as some sort of property acessor

Augusto Moura augusto.borgesm at gmail.com
Wed Mar 8 00:33:37 UTC 2017

Before I open any suggestion for my ideia in the [bind-operator](//
github.com/tc39/proposal-bind-operator) proposal, I like to know you guys'
opinion about, and if even it is in the scope of the current proposal.

There are moments when we want to export instances methods binded with
their objects, like `num.toFixed.bind (num)`, most of the time to be used
as high order functions. With the current proposal the best we can get is
`num::num.toFixed`, what seems a bit weird and counterintuitive.

For example, let's say that for an unknow reason we want to copy a array to
another using the `push` method. There are a few ways:
``` .javascript
const unary = fun => a => fun(a);
const arr = [];

// With arrow functions
[1, 2, 3, 4].map(n => arr.push(n));

// With binding
[1, 2, 3].map(unary(arr.push.bind(arr)));

And again, the best way of doing this with the current proposal is
something like:
``` .javascript
[1, 2, 3].map(unary(arr::arr.push));

My idea, similar to double colon operator in Java 8, is that the expression
`foo::bar` translates to: access `bar` in `foo`, if it is a function,
returns the function binded to foo.

``` .javascript
// The two expressions below produce the same function

// So in our example instead of using something like
[1, 2, 3].map(arr::arr.push);
// we can use
[1, 2, 3].map(arr::push);

And yeah, it just decreases one word. However imagine the symbol as acessor
instead of a operator, leaves the things really less confusing in my

It can be useful for React applications too:
class Button extends React.Component {
  alertText() {

  render() {
    // Instead of `this::this.alertText`
    return <button onClick={this::alertText} />;

Imo, my idea has a different motivation e use case than the current
proposal and because of that, needs to be splitted with the original. We
can discuss other symbols than the `::`, but I personally think that it
fits perfectly and make some neat code when composing high order functions.

Augusto B. Moura
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20170308/c0734874/attachment.html>

More information about the es-discuss mailing list