js call方法改变this指向

53 阅读1分钟
    //定义函数
    function fn(n1, n2) {
      console.log(this);
      console.log(n1, n2)
    }
    //调用call()方法
    fn.call();//=>this:window;
    let obj = { fn: fn };
    fn.call(obj);//=>this:obj;n1,n2:undefined
    fn.call(1, 2);//=>this: 1;n1=2,n2=undefined;
    fn.call(obj, 1, 2);//=>this: obj;n1=1,n2=2;