我有以下内容…
export class Puzzle {
pieces : Piece[];
orderedPieces : Piece[][];
constructor(width: number, height: number){
this.height = height, this.width = width;
let total : number = width * height;
this.pieces = new Array<Piece>(total);
this.orderedPieces = new Piece[height][width]; // Doesn't work what do I put here?
}
...
}
如何在Typescript中声明这样的东西?
错误……
无法读取未定义属性'2'
没有办法(我知道)在一行javascript中实例化一个多维数组。你需要自己创建所有的数组,比如:
this.orderedPieces = new Array(this.height);
for (let i = 0; i < this.height; i++) {
this.orderedPieces[i] = new Array(this.width);
}