/* 普通调用 this -->window
function fn(){
console.log(this) // window
}
var fn = ()=>{
console.log(this) // window
}
/* 对象调用 this -->对象
var obj = {
name:‘zs’,
say:function(){
console.log(this.name);
}
}
/* new 函数 this --> 实例
function Fn(name){
this.name = name;
this.say=function(){
console.log(this);
}
}
var f1 = new Fn(‘zs’);
/* 事件处理函数 this – > 事件源
btnObj.onclick = function(){
console.log(this)
}
/* 定时器的回调函数 this --> window
setInterval(function(){
console.log(this)
},30)
上下文调用模式
bind()方法:
1.不会调用函数,会返回一个新的函数
2. 新函数中this指向传入的值 传参: 第一个是传谁,this指向谁, 后面的参数,需要什么传什么
判断this指向的两条规则:
1. this在哪个函数中
2. 这个函数是被哪种调用模式调用的
function fn(){
console.log(this)
}
fn();
obj ={name:'zs'}
var o = fn.bind(obj)
o(); // 此时指向obj
*/