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