-
Notifications
You must be signed in to change notification settings - Fork 892
Open
Description
var fullname = 'a';
var obj = {
fullname: 'b',
prop: {
fullname: 'c',
getFullname: function() {
return this.fullname;
}
}
};
console.log(obj.prop.getFullname()); // c
var test = obj.prop.getFullname;
console.log(test()); // a
每日一题会在下午四点在交流群集中讨论,五点小程序中更新答案
欢迎大家在下方发表自己的优质见解
二维码加载失败可点击 小程序二维码
扫描下方二维码,收藏关注,及时获取答案以及详细解析,同时可解锁800+道前端面试题。
XingXiangrong124
Activity
Genzhen commentedon Jun 22, 2020
答案
c a
解析
this
指向的是函数的执行环境,this
取决于其被谁调用了,而不是被谁定义了。console.log()
语句而言,getFullName()
是作为obj.prop
对象的一个方法被调用的,因此此时的执行环境应该是这个对象。另一方面,但getFullName()
被分配给test
变量时,此时的执行环境变成全局对象(window
),原因是test
是在全局作用域中定义的。因此,此时的this
指向的是全局作用域的fullname
变量,即a。yaooooooooo commentedon Jul 21, 2020
?????答不对题啊........
Genzhen commentedon Jul 22, 2020
@yaooooooooo 改过来了哈,感谢指正