Vic*_*huk 127 javascript ecmascript-harmony ecmascript-next
如您所知,有一个.bind()功能快捷方式的建议,所以您可以写:
::this.handleStuff
Run Code Online (Sandbox Code Playgroud)
它将在es5中像那样工作:
this.handleStuff.bind(this)
Run Code Online (Sandbox Code Playgroud)
我的问题是:这样可以传递参数吗?
我的意思是用上述快捷方式编写这个方法:
this.handleStuff.bind(this, 'stuff')
Run Code Online (Sandbox Code Playgroud)
这在React中是一个非常常见的模式,所以稍微缩短它会很好.
Ber*_*rgi 149
方法提取
::obj.method ? obj.method.bind(obj)
Run Code Online (Sandbox Code Playgroud)"虚方法"调用
obj::function ? function.bind(obj)
obj::function(…) ? function.call(obj, …)
Run Code Online (Sandbox Code Playgroud)它们都没有部分应用.对于你想要的,你应该使用箭头功能:
(...args) => this.handleStuff('stuff', ...args) ? this.handleStuff.bind(this, 'stuff')
Run Code Online (Sandbox Code Playgroud)