此刻我正在edx.org關於Typescript2做一個教程。 但我需要一些幫助,自我評估實驗室:骰子滾動應用打字稿
「現在你已經學會了如何使用打字稿,你會做一個簡單的擲骰應用以下參數的基礎知識:
使用在應用程序中以下幾種類型:數組,數字,字符串,布爾
使用枚舉聲明可能捲起值(提示:他們不必爲數字)
使用一個類來創建。你的死亡,包括需要的CSS樣式的鍵入屬性(長度,寬度,邊界,顏色)以及骰子中包含的值。
創建一個界面來描述你的骰子的類型。
使用dieRoller類擴展類以創建滾動骰子的方法。
創建一個函數來使用dieRoller類實例化骰子,使用constructor()函數綁定元素。
創建一個按鈕,一次擲出所有的骰子。
成品應該結束了尋找這樣的事情,而且當按下「擲骰子」按鈕應該隨機生成,從枚舉不同的價值爲每個單獨的芯片。」
應該有四個骰子和然後按一個按鈕(全部在一行)
我的問題是,我無法創建只有一個按鈕或我不能使用rolleDice()(如果我聲明類之外的按鈕...通常是一個可能會在外面聲明,並創建一個類的實例...但不知道如何做到這一點,因爲類有一個帶參數的構造函數)所以,也許你有一個想法...或者我只是錯過了任務? :/
謝謝!
class diceRoller {
span: Element;
constructor(span: Element) {
this.span = span;
}
rolleDice(diceValue: number): boolean {
(<HTMLElement>this.span).textContent = diceValues[diceValue];
return true;
}
}
class dice extends diceRoller {
button: Element = document.createElement('button');
constructor(span: Element) {
super(span);
(<HTMLElement>span).style.cssText = "border: 5px solid black; display: inline-block; height: 50px; width: 50px; margin: 2px";
this.button.textContent = "Role Dice";
document.body.appendChild(this.button);
}
}
enum diceValues {
one,
two,
three,
four,
five,
six
}
interface diceTypes {
span: Element;
}
let Elements: Array<diceTypes> = [];
for (let index: number = 0; index < 5; index++) {
Elements.push({
'span': document.createElement('span'),
});
}
let getRandomIntInclusive: Function = (min, max) => {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
}
Elements.map((elem, index) => {
let cube = new dice(elem.span);
let button: Element = document.createElement('button');
document.body.appendChild(elem.span);
});