document.getElementById( 'div1' ).onclick = function(){ console.log( this.id ); var func = function(){ console.log ( this.id ); } func();}; 請問上面func中的this為什么會指向window?我的理解是func執行時所在的上下文環境是onclick函數。所以this應該指向該函數,同樣的下面的函數我倒是很好理解,因為定時器延遲了函數的執行,所以里面的this應該指向window,但是上面的函數沒有定時器啊function foo() { setTimeout(() => { console.log('id:', this.id); }, 100);}var id = 21;foo(); //21(沒有用call)
函數中this指向問題
牛魔王的故事
2019-02-18 16:13:13