本文介绍了JavaScript双冒号(绑定运算符)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如您所知,有一个关于 .bind()
函数的快捷方式的建议,所以你可以写:
As you know, there is a proposal for a shortcut for .bind()
function, so you can write:
::this.handleStuff
它将会在es5中这样工作:
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')
这是React中非常常见的模式,所以稍微缩短它会很好。
It's a pretty common pattern in React, so it would be nice to shorten it a little.
推荐答案
没有。 ()有两种形式:
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')
这篇关于JavaScript双冒号(绑定运算符)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!