改变函数内部this的指向 js提供了三种方法 call(), apply(), bind()
1. call(‘this想指向的对象’, ‘形参1’, ‘形参2’, ·····)
- call 可以调用函数
- 可以改变this的指向
- 不传参数,或者传 null, undefined, 函数中的 this 指向 window 对象
const obj = {
name: 'wenqing'
};
function fn(x, y) {
console.log(this);
console.log(x + y);
}
fn(); // this 指向window 输出 NaN
fn.call(null, 2, 4) // this 指向 window 输出 6
fn.call(obj, 1, 2); // this 指向obj对象 输出 3
- call 的主要作用可以实现继承
function Father(uname, age, sex<