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参数是一个假参数,应该如何理解,为什么将this的类型定义成void就不会报错?
料青山看我应如是
相关分类