如您所知,有一个关于.bind()
函数的快捷方式的提议,因此您可以这样写:
::this.handleStuff
它会像在 es5 中那样工作:
this.handleStuff.bind(this)
我的问题是:是否可以通过这种方式传递参数?
我的意思是用前面提到的快捷方式写这个:
this.handleStuff.bind(this, 'stuff')
这是 React 中非常常见的模式,因此将其缩短一点会很好。
最佳答案
没有。 bind operator ( spec proposal ) 有两种形式:
方法提取
::obj.method ≡ obj.method.bind(obj)
“虚方法”调用
obj::function ≡ function.bind(obj) obj::function(…) ≡ function.call(obj, …)
它们都没有 partial application .对于你想要的,你应该使用箭头函数:
(...args) => this.handleStuff('stuff', ...args) ≡ this.handleStuff.bind(this, 'stuff')
关于JavaScript 双冒号(绑定(bind)运算符),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31220078/