【发布时间】:2020-01-23 13:15:34
【问题描述】:
所以我想知道如何在类构造函数中初始化数组:
class test{
constructor(){
//here i want to initialize a new array called "array" from the class "class2"
let array = [new class2()]; //is this the way to do it?
}
add(x){
for(let i=0; i<array.length; i++){
if(array[i] == null){
array[i] = x;
break;
}
}
}
}
我想为该数组提供另一个类的一些值,例如 class2 有另一个带有一些值的构造函数(比如说名字和年龄)
【问题讨论】:
-
您可以使用 class2 扩展您的类并使用
super()函数来委托来自 class2 的值
标签: javascript arrays object constructor