js call

1.作用

函数call方法,可以指定该函数内部this的指向(即函数执行时所在的作用域),然后在所指定的作用域中,调用该函数。

Fn.call(obj,arg1,arg2,...);  

Fn的this === obj                    //obj写成this的话   this就等于原本的

arguments = [arg1,arg2,...]    //后面arg是 Fn的参数  也就是Fn原本的参数, 现在变成 arg1,arg2,......

2.全局

var o = {};

var f = function (){
  return this;   //全局
};

f() === this // true
f.call(o) === o // true

3.参数为null或undefined,则等同于指向全局对象

var n = 123;
var o = { n : 456 };

function a() {
  console.log(this.n);
}

a.call() // 123
a.call(null) // 123
a.call(undefined) // 123
a.call(window) // 123
a.call(o) // 456
原文地址:https://www.cnblogs.com/zycbloger/p/5567454.html