亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

關于es6箭頭函數的一些小問題

關于es6箭頭函數的一些小問題

FFIVE 2018-10-10 13:14:03
var id =21;        function testThis(){            setTimeout(function(){                console.log(this.id);            },1000);        }        let testThis2 = ()=>{            setTimeout(function(){                console.log(this.id);            },1000);        }        function testThis3(){            setTimeout(()=>{                console.log(this.id);            },1000);        }        testThis();        testThis2();        testThis3();        testThis.call({id:42});        testThis2.call({id:42});        testThis3.call({id:42});為什么 testThis2.call({id:42}); console的是21呢。
查看完整描述

1 回答

?
30秒到達戰場

TA貢獻1828條經驗 獲得超6個贊

因為看this首先看它所在的function, testThis2表面上自身是用了箭頭函數,但只能說,testThis2的函數執行內容里的this可以按.call({id:42})綁定,而setTimeout里面還有一個function,里面的this是要看這個內部function的,不會去看testThis2的this的。
所以才有testThis3的做法嘛~

查看完整回答
反對 回復 2018-11-27
  • 1 回答
  • 0 關注
  • 537 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號