Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
288 views
in Technique[技术] by (71.8m points)

一道关于js this指向问题

    var name = 'the window'
    var ob = {
        name: 'my object',
        getName: function(){
            return this.name;
        }
    }
    var a;
    console.log(ob.getName());  //my object
    console.log((ob.getName)());  //my object
    

为什么第二个显示my object? 立即执行函数this不是指向window吗?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

image.png
原文

image.png
原文

这是根据网上文章的一些解释

如果赋值给a,函数this就指向window

var a = ob.getName;
a(); //the window

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...