<script type="text/javascript"> var fullname = 'John Doe'; var obj = { fullname: 'Colin Ihrig', prop: { fullname: 'Aurelio De Rosa', getFullname: function() { return this.fullname; } } }; var test = obj.prop.getFullname; console.log((test)()); console.log((obj.prop.getFullname)());// John Doe Aurelio De Rosa
1 回答
茅侃侃
TA贡献1842条经验 获得超21个赞
this
指向问题,两次运行的this指向不是同一个对象
var fullname = 'John Doe';
var obj = {
fullname: 'Colin Ihrig',
prop: {
fullname: 'Aurelio De Rosa',
getFullname: function() {
console.log(this);//加上这一句 看看输出的this
return this.fullname;
}
}
};
var test = obj.prop.getFullname;
console.log((test)());
console.log((obj.prop.getFullname)());
添加回答
举报
0/150
提交
取消