第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

typescript中文網(wǎng)函數(shù)一節(jié)關(guān)于this的講解

typescript中文網(wǎng)函數(shù)一節(jié)關(guān)于this的講解

森林海 2019-01-04 21:44:16
let deck = {    suits: ["hearts", "spades", "clubs", "diamonds"],    cards: Array(52),    createCardPicker: function() {        // NOTE: the line below is now an arrow function, allowing us to capture 'this' right here         return () => {            let pickedCard = Math.floor(Math.random() * 52);            let pickedSuit = Math.floor(pickedCard / 13);            return {suit: this.suits[pickedSuit], card: pickedCard % 13};         }     } }let cardPicker = deck.createCardPicker();let pickedCard = cardPicker(); alert("card: " + pickedCard.card + " of " + pickedCard.suit);教程說this和this.suits[pickedSuit]的類型為any,會報錯。然后提供了一種解決方案function f(this: void) {    // make sure `this` is unusable in this standalone function}教程說這里的this參數(shù)是一個假參數(shù),應該如何理解,為什么將this的類型定義成void就不會報錯?
查看完整描述

1 回答

?
料青山看我應如是

TA貢獻1772條經(jīng)驗 獲得超8個贊

其實就是一種斷言而已了,就是開發(fā)者斷言當前的作用域下的 this 指向某個類型,如果你不聲明的話,那肯定是 any 類型。

對于你說的第二個問題,為什么設(shè)置為 void 就不會報錯,前提是你不在函數(shù)中使用 this 才可以,如果使用 this 的話,諸如 this.a 之類的代碼均會報錯。


查看完整回答
反對 回復 2019-01-04
  • 1 回答
  • 0 關(guān)注
  • 446 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號