老师,请帮忙解析下这个题目,谢谢
相关代码:
<script>
function Timer() {
this.s1=0;
this.s2=0;
// 箭头函数
setInterval(() => this.s1++, 1000);
// 普通函数
setInterval(function () {
this.s2++;
},1000)
}
let timer=new Timer();
setTimeout(()=>console.log('s1',timer.s1),2000);
setTimeout(()=>console.log('s2',timer.s2),2000);
</script>问题描述:
老师,请问 箭头函数中,this向外查找,所以this指实例,this.s1每秒能++,2秒后输出3,
但是普通函数中,this指向window对象,为何2秒后输出的是0而不是undefined呢,这里弄不明白,请帮忙分析下,谢谢
10
收起
正在回答 回答被采纳积分+1
1回答


恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星