index.ts 如下:
let deck = {
suits: ['hearts', 'spades', 'clubs', 'diamonds'],
cards: Array(arrayLength:52),
createCardPicker: function() {
return () => {
let pickedCard = Math.floor(x:Math.random() * 52)
let pickedSuit = Math.floor(x:pickedCard / 13)
return {
suit: this.suits[pickedSuit],
card: pickedCard % 13
}
}
}
}
let cardPicker = deck.createCardPicker()
let pickedCard = cardPicker()
console.log('card: ' + pickedCard.card + ' of ' + pickedCard.suit)
tsc index.ts编译:报错:
index.ts(3,27): error TS1005: ',' expected.
index.ts(6,38): error TS1005: ',' expected.
index.ts(7,35): error TS1005: ',' expected.
问题答案:
arrayLength: x: 这些貌似都是 webstome自动生成的提示性的内容,与程序本身无关.
不是编译问题,语法错误。没有
Array(arrayLength:52)
这种写法的,后面的x:Math.random()
也一样