call apply bind

 call apply:IE8+

     相同点:不创建新函数,临时借(调)用函数,临时替换函数中的this为指定对象。   

     不同点:传递给借用的函数的参数:

              call,要求每个参数独立传入

              apply,要求所有参数用数组或集合方式整体传入。

    bind: IE9+

         相同:都是为了替换函数中的this为指定对象。

         不同:

              1.创建一个新函数。

               2.永久绑定this为指定对象,且也可永久绑定部分参数值。一旦绑定无法用call(apply)替换.

    

涉及到知识点:

       var max=Math.max(a,b,c,...); 获得参数中的最大值

       var min=Math.min(a,b,c,...); 获得参数中的最小值

           问题: max和min不支持获取数组中的最大值

           解决Math.max.apply(null,[a,b,c,...]);

                   Math.max.apply(null,arr);//获得arr数组中最大值

                  apply:调用函数,并。。。

                   同时将数组参数,打散后传递给之前的函数

  eg:获得数组中最大值和最小值  (number 没有 max 方法, Math 有,可以借助 call 或者 apply 方法  )        

//apply
var num=[7,88,130,-240,22];
var maxInNumbers=Math.max.apply(Math,num);
console.log(maxNumbers);//130
//call
var maxInNumbers=Math.max.call(Math,7,88,130,-240,22);
console.log(maxInNumbers);//130

eg:类(伪)数组使用数组方法 ( arguments 对象   getElementsByTagName , document.childNodes)

var  aname = Array.prototype.slice.call(document.getElementsByTagName("*")/arguments);

它们返回对象都属于伪数组。不能应用 Array下的 push , pop,unshift,shift 等方法。

可以通过 Array.prototype.slice.call 转换为真正的数组的带有 length 属性的对象,这样 aname就可以用 数组的所有方法了。

        

    

原文地址:https://www.cnblogs.com/lengkafei/p/5605355.html