为了账号安全,请及时绑定邮箱和手机立即绑定

为什么settimeout中的函数this指向window?

为什么settimeout中的函数this指向window?

桃花长相依 2018-10-10 13:14:58
var obj = {    name: 'name',    foo: function () {        console.log(this); // Object {name: "name"}        setTimeout(function () {            console.log(this);  // Window        }, 1000);    },    foo2: function () {        console.log(this); // Object {name: "name"}        setTimeout(() => {            console.log(this);  // Object {name: "name"}        }, 2000);    }}为什么settimeout中的函数this指向window?而箭头函数this指向Object是因为settimeout是window对象的方法还是说当做直接调用一个函数?
查看完整描述

2 回答

?
zangbianxuegu

TA贡献1条经验 获得超0个赞

因为 setTimeout 这个方法是挂载到 window 对象上的。setTimeout 执行时,执行回调函数,回调函数中的 this 指向调用 setTimeout 的对象,window

查看完整回答
反对 回复 2020-09-24
  • 2 回答
  • 0 关注
  • 1564 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信