Javascript Typescript 2d数组定义引发错误
我来自一个强大的JavaScript背景,试图学习TypeScript。我遇到了一个我知道可以在JavaScript中使用但不能在TypeScript中使用的问题: 我试图用一个5的矩阵或2d数组初始化一个类成员;这是我的密码:Javascript Typescript 2d数组定义引发错误,javascript,typescript,Javascript,Typescript,我来自一个强大的JavaScript背景,试图学习TypeScript。我遇到了一个我知道可以在JavaScript中使用但不能在TypeScript中使用的问题: 我试图用一个5的矩阵或2d数组初始化一个类成员;这是我的密码: private rows: number; private cols: number; private data: number[][]; constructor(rows: number, cols: number) { this.rows = rows;
private rows: number;
private cols: number;
private data: number[][];
constructor(rows: number, cols: number) {
this.rows = rows;
this.cols = cols;
this.data = [];
// init matrix with fives
for (let i: number = 0; i < this.rows; i++) {
this.data[i] = [];
for (let j: number = 0; j < this.cols; i++) {
this.data[i][j] = 5; // this is line 21
}
}
}
我已经看了很长一段时间了,似乎其他人没有这个问题。有人知道怎么解决这个问题吗
顺便说一句:我在纯JS中尝试了同样的代码,它运行得很好,没有任何错误。似乎我在第二个循环中也增加了
for (let i: number = 0; i < this.rows; i++) {
this.data[i] = [];
for (let j: number = 0; j < this.cols; j++) { //j here
this.data[i][j] = 5; // this is line 21
}
}
for(设i:number=0;i
在第二个循环中,i似乎也是递增的
for (let i: number = 0; i < this.rows; i++) {
this.data[i] = [];
for (let j: number = 0; j < this.cols; j++) { //j here
this.data[i][j] = 5; // this is line 21
}
}
for(设i:number=0;i
我看到您将数据变量声明为2d数组,然后在构造函数之后将其初始化为1d数组。我认为在打字稿中你不应该那样做 我看到您将数据变量声明为2d数组,然后在构造函数之后将其初始化为1d数组。我认为在打字稿中你不应该那样做 不是类型脚本问题,你在第二个循环中增加iî,应该是jn而不是类型脚本问题,你在第二个循环中增加iî,应该是j