Victor Marchuk Victor Marchuk - 1 year ago 397
Javascript Question

JavaScript double colon (bind operator)

As you know, there is a proposal for a shortcut for

function, so you can write:


and it will work like that in es5:


My question is: will it be possible to pass arguments this way?

I mean a way of writing this with the aforementioned shortcut:

this.handleStuff.bind(this, 'stuff')

It's a pretty common pattern in React, so it would be nice to shorten it a little.

Answer Source

No. The bind operator (spec proposal) comes in two flavours:

  • Method extraction

    ::obj.method     ≡ obj.method.bind(obj)
  • "virtual method" calls

    obj::function    ≡ function.bind(obj)
    obj::function(…) ≡, …)

Neither of them feature partial application. For what you want, you should use an arrow function:

(...args) => this.handleStuff('stuff', ...args) ≡ this.handleStuff.bind(this, 'stuff')