Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

ecmascript harmony - JavaScript double colon (bind operator)

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.

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...