JavaScript 双冒号(绑定操作符)

正如你所知道的,有一个关于 .bind()函数的快捷方式的建议,所以你可以写:

::this.handleStuff

它在 es5中是这样工作的:

this.handleStuff.bind(this)

我的问题是: 这样传递论点有可能吗?

我的意思是用上面提到的快捷方式来写这篇文章:

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

这是一个非常普遍的模式在反应,所以它将是不错的缩短一点。

38219 次浏览

不,绑定操作符(规格建议书)有两种口味:

  • 方法提取

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