Javascript 中 class 个对象的二维数组
2 dimensional array of class objects in Javascript
试图在 JS 中创建一个包含 class 个对象的数组。我不知道 Javascript 如何处理这个问题,但不是 10x10 网格,所有数字都设置为 10 而不是我要分配的 i 和 j 值。
class Box {
constructor(width, height, x, y, inside) {
this.width = width;
this.height = height;
this.x = x;
this.y = y;
this.inside = inside;
}
getHeight() {
return this.height;
}
setHeight(newHeight) {
this.height = newHeight;
}
let boxes = [
[]
];
let testBox = new Box(1, 1, 1, 1, "Test")
for (let i = 0; i < 11; i++) {
for (let j = 0; j < 11; j++) {
boxes[i[j]] = new Box(i, j, i, j, "Test");
}
}
console.log(testBox.getHeight()); //Working Example
console.log(boxes[3[3]].getHeight()); //outputs 10?
console.log(boxes[4[6]].getHeight()); //outputs 10?
我在评论中写的例子
class Box {
constructor(width, height, x, y, inside) {
this.width = width;
this.height = height;
this.x = x;
this.y = y;
this.inside = inside;
}
getHeight() {
return this.height;
}
setHeight(newHeight) {
this.height = newHeight;
}
}
let boxes = [];
for (let i = 0; i < 11; i++) {
for (let j = 0; j < 11; j++) {
boxes[i] = [...(boxes[i] ? boxes[i] : []),
new Box(i, j, i, j, "Test")
];
}
}
console.log(boxes[3][3].getHeight());
console.log(boxes[4][6].getHeight());
据我了解,您已经声明了一个 class 框,并且您想要创建这个 class 的对象数组。考虑到这种情况,
您的代码有语法错误:数组和循环必须在 class 定义之外。
既然你想制作一个对象数组,它不是二维数组,它只是一个一维数组array.So代码应该如下所示
class Box {
constructor( width, height, x ,y, inside) {
this.width = width;
this.height = height;
this.x = x;
this.y = y;
this.inside = inside;
}
getHeight(){
return this.height;
}
setHeight(newHeight){
this.height = newHeight;
}}
let boxes = [];
for(let i = 0; i < 11; i++){
boxes.push(new Box(i,i+2,i,i+2,"Test"));
}
for(var cnt in boxes)
console.log(boxes[cnt]);
试图在 JS 中创建一个包含 class 个对象的数组。我不知道 Javascript 如何处理这个问题,但不是 10x10 网格,所有数字都设置为 10 而不是我要分配的 i 和 j 值。
class Box {
constructor(width, height, x, y, inside) {
this.width = width;
this.height = height;
this.x = x;
this.y = y;
this.inside = inside;
}
getHeight() {
return this.height;
}
setHeight(newHeight) {
this.height = newHeight;
}
let boxes = [
[]
];
let testBox = new Box(1, 1, 1, 1, "Test")
for (let i = 0; i < 11; i++) {
for (let j = 0; j < 11; j++) {
boxes[i[j]] = new Box(i, j, i, j, "Test");
}
}
console.log(testBox.getHeight()); //Working Example
console.log(boxes[3[3]].getHeight()); //outputs 10?
console.log(boxes[4[6]].getHeight()); //outputs 10?
我在评论中写的例子
class Box {
constructor(width, height, x, y, inside) {
this.width = width;
this.height = height;
this.x = x;
this.y = y;
this.inside = inside;
}
getHeight() {
return this.height;
}
setHeight(newHeight) {
this.height = newHeight;
}
}
let boxes = [];
for (let i = 0; i < 11; i++) {
for (let j = 0; j < 11; j++) {
boxes[i] = [...(boxes[i] ? boxes[i] : []),
new Box(i, j, i, j, "Test")
];
}
}
console.log(boxes[3][3].getHeight());
console.log(boxes[4][6].getHeight());
据我了解,您已经声明了一个 class 框,并且您想要创建这个 class 的对象数组。考虑到这种情况, 您的代码有语法错误:数组和循环必须在 class 定义之外。
既然你想制作一个对象数组,它不是二维数组,它只是一个一维数组array.So代码应该如下所示
class Box {
constructor( width, height, x ,y, inside) {
this.width = width;
this.height = height;
this.x = x;
this.y = y;
this.inside = inside;
}
getHeight(){
return this.height;
}
setHeight(newHeight){
this.height = newHeight;
}}
let boxes = [];
for(let i = 0; i < 11; i++){
boxes.push(new Box(i,i+2,i,i+2,"Test"));
}
for(var cnt in boxes)
console.log(boxes[cnt]);