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

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

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,會(huì)報(bào)錯(cuò)。然后提供了一種解決方案function f(this: void) {    // make sure `this` is unusable in this standalone function}教程說這里的this參數(shù)是一個(gè)假參數(shù),應(yīng)該如何理解,為什么將this的類型定義成void就不會(huì)報(bào)錯(cuò)?
查看完整描述

1 回答

?
料青山看我應(yīng)如是

TA貢獻(xiàn)1772條經(jīng)驗(yàn) 獲得超8個(gè)贊

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

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


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

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號

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