有没有办法同时绑定和调用?

时间:2016-03-21 22:27:05

标签: javascript

说我有这样的代码:

var boundFilter = this.filterCouriers.bind(this);
boundFilter();

有没有办法在一行中调用它?

(基本上我想知道是否有办法调用绑定方法而不必将其存储在变量中。)

3 个答案:

答案 0 :(得分:6)

只需使用电话

即可
this.filterCouriers.call(this);
  

call()方法使用给定的值和单独提供的参数调用函数。 MDN - call

答案 1 :(得分:3)

bind会返回一个函数,因此您可以立即调用它:

this.filterCouriers.bind(this)();

答案 2 :(得分:1)

this.filterCouriers.bind(this)();

这应该可以解决问题。如果没有试试这个:

(this.filterCouriers.bind(this))();