bind (), the difference between call (), apply () method is what is?

bind (), the difference between call (), apply () method is what is?

  • In common: Change this point, any calls are not at work

  • bind () to change the point of this, it does not call the function that returns a new function

        var o ={a:'abc'};
        var fn1 = fn.bind(o);//this指向o  相当o.fn
        fn1();//
  • call () function call to change and this point determines the data type ---

        var arr = [1,2,4]
        var str = '1231';
        console.log(Object.prototype.toString.call(arr));//返回Array数据类型
        console.log(Object.prototype.toString.call(str));返回String数据类型
  • apply () to change this point and calls the function, parameter is an array of behind the show

            //求一数组中的最大值
            var  arr = [2,13,30,1,4];
            console.log(Math.max.apply(null,arr));//利用apply()可以把数组展开单独传参
            console.log(Math.max.apply(Math,arr));
              //把数组中的元素一行展示
            console.log.apply(console,arr);//指向可以是null,主要是为了把数组展开传给console

Guess you like

Origin www.cnblogs.com/zcsmile/p/10961983.html