前端面试题--技术无忧(tech51.cn)
var name='tech51.cn';
function introduce(){
    console.log('Hello,My name is ', this.name);
}
const Tom = {
    name: 'TOM',
    introduce: function(){
        setTimeout(function(){
            console.log(this)
            console.log('Hello, My name is ',this.name);
        })
    }
}
const Mary = {
    name: 'Mary',
    introduce
}
const Lisa = {
    name: 'Lisa',
    introduce
}

Tom.introduce();
setTimeout(Mary.introduce, 100);
setTimeout(function(){
    Lisa.introduce();
},200);

解析: Tom.introduce()执行: console位于setTimeout的回调函数中,回调函数的this指向window.
Mary.introduce直接作为setTimeout的函数参数,会发生隐式绑定丢失,this为默认绑定.
Lisa.introduce执行虽然位于setTimeout的回调函数中,但保持xxx.fn模式,this为隐式绑定.

答案:

Window {…}, Hello, My name is tech51.cn, Hello,My name is tech51.cn, Hello, My name is Lisa

编号: 106