代码演示

  1. -- apply 临时改变函数的this指向,用数组传所有参数;
  2. Math.max.apply(null,[1,4,6,2]);
  3. -- call 临时改变函数的this指向,一次传所有参数
  4. Math.max.call(null,1,4,6,2);
  5. -- bind 改变函数的this指向,多次传所有参数;返回永久改变this指向的函数;
  6. var arr=[1,10,5,8,12];
  7. var max=Math.max.bind(null,arr[0])
  8. max=Math.max.bind(null,arr[1],arr[2])
  9. max=Math.max.bind(null,arr[4])
  10. console.log(max()); //12,分两次传参

参考

彻底弄懂bind,apply,call三者的区别 Function.prototype.bind() Function.prototype.call() Function.prototype.apply()