140

As you know, there is a proposal for a shortcut for .bind() function, so you can write:

::this.handleStuff

and it will work like that in es5:

this.handleStuff.bind(this)

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.

Michał Perłakowski
  • 88,409
  • 26
  • 156
  • 177
Victor Marchuk
  • 13,045
  • 12
  • 43
  • 67

1 Answers1

165

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(…) ≡ function.call(obj, …)
    

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')
Michał Perłakowski
  • 88,409
  • 26
  • 156
  • 177
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • 6
    (There was [some discussion](https://github.com/tc39/proposal-bind-operator/issues/37) about supporting partial application) – Bergi Aug 22 '16 at 12:34