TypeScript語言使用Dictionary
前言:ts語言其實並沒用提供直接的字典關鍵字讓我們直接使用字典,不過字典的原理是key —value鍵值對,我們據此實現ts的字典功能
export class Player
{
public userid:number;
public score:number;
public name:string;
public online:boolean;
public ready:boolean;
public seatindex:number;
constructor()
{
}
}
class GameMain{
private playerNum:number = 6;
private playerInfoDic:{[playerId:number] : Player} = {}; //宣告字典
constructor()
{
Laya.init(600,400, WebGL);
for(let i = 0; i < this.playerNum; i++)//向字典裡新增資料(ps:this.seatsData也是一個字典)
{
let player = new Player();
player.userid = this.seatsData[i]["userid"];
player.ip = this.seatsData[i]["ip"];
player.score = this.seatsData[i]["score"];
player.name = this.seatsData[i]["name"];
player.online = this.seatsData[i]["online"];
player.ready = this.seatsData[i]["ready"];
player.seatindex = this.seatsData[i]["seatindex"];
this.playerInfoList[player.userid] = player;
}
}