对call的理解

f.call(o,para1,para2,....);

f: 是调用的函数

o:是函数f中this指向的对象

para1,para2,...:是调用 f 函数时传入的实参

 eg:

    function Father(name){
      this.name = name || '名字'     
    }
    function Son(){};
    let son = new Son();
    Father.call(son)
    console.log(son.name) //名字
    console.log(Son.name) //undefined
   

 Father.call(son)中call的作用是,调用Father函数,使Father函数中的this指向为son,也就是改变了Father函数中this的指向。

猜你喜欢

转载自www.cnblogs.com/fewhj/p/10371327.html