鍍金池/ 問答/HTML/ 什么情況下會出現(xiàn)this指向是undefined

什么情況下會出現(xiàn)this指向是undefined

如下兩個例子,一個是具名函數(shù),一個匿名函數(shù),為什么不同結(jié)果

var a = {
    b: function() {
        function a() {
            console.log(this.a);
        }
        a();
    }
};
a.b();// Uncaught TypeError: Cannot read property 'a' of undefined
var a = {
    b: function b() {
        function a() {
            console.log(this.a);
        }
        a();
    }
};
a.b();// undefined
回答
編輯回答
愛礙唉

匿名函數(shù)應(yīng)該不會影響默認(rèn) this ,影響默認(rèn) this 的是 strict mode ,是否使用了 use strit。

2017年3月19日 10:49
編輯回答
替身

應(yīng)該都是{b: ?} 可能你是嚴(yán)格模式

2017年5月21日 18:58