js的call,apply,bind

参考链接:https://www.cnblogs.com/Shd-Study/archive/2017/03/16/6560808.html

总结:

  • 三者均改变函数的this的指向
  • bind返回函数,需要()再执行
  • apply传参需要是数组形式

Array.prototype.join.call('this is a string', '_');

Array.prototype.join.bind('this is a string', '_')();

Array.prototype.join.apply('this is a string', ['_']);

// "t_h_i_s_ _i_s_ _a_ _s_t_r_i_n_g"

猜你喜欢

转载自my.oschina.net/littleFaye/blog/1818513