this指向与更改this指向的方法
&1.this指向window
function apple(){
SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。console.log(this);//window
}
apple();
&2.this指向实例化对象
function Person(age,name){
this.age = age;
this.name = name;
console.log(this)//Person
}
var p1=new Person(18,"dzj");
var p2 = new Person(20,"dfj");
&3.this指向该方法所属的对象
var obj = {
fn:function(){
console.log(this)//obj
}
}
obj.fn();
&4.通过事件绑定的方法, 此时 this 指向 绑定事件的对象
<button id="btn">点击</button>
<script>
var myObtn=document.getElementById("btn");
myObtn.onClick = function(){
console.log(this)//<button id="btn">点击</button>
}
</script>
&5.定时器函数,此时this指向window
setInterval(function () {
console.log(this)
},2000)
*********************更改this指向的方法***********************

更多精彩