JS中call()要领

// arguments变量的写法

function sortNumbers(){
    return Array.prototype.slice.call(argarguments).sort();
    //call 转变this的指向
}

// obj1.(method).call(obj2,arg1,arg2)
// call的作用就是把obj1的要领放到obj2上运用,背面arg1…这些作为obj1的参数传入


function add(x,y){
    console.log(x+y);
}
function minus(x,y){
    console.log(x-y)
}
add.call(minus,10,6);//16

function myFun1(){
    this.name = 'xiaolei';
    this.myTxt = function(txt){
        console.log('I am',txt);
    }
}
function myFun2(){
    myFun1.call(this);
}
var myFun3 = new myFun2();
myFun3.myTxt('handan');//I am handan
console.log(myFun3.name);//xiaolei
    原文作者:阳光Angel
    原文地址: https://segmentfault.com/a/1190000018354010
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞