call强制改变this的指向

65 阅读1分钟

call:执行一次后改变this指向

参数一:this指向
剩余参数:传入参数(实参)
示例1:
function fn() {
    console.log(this)
}
fn()  //默认指向window
fn.call(1)

输出结果:

image.png

示例2:
function fn(a,b) {
    console.log(this,a+b)
}
fn.call(1,2,3)

输出结果:

image.png