如何在数组中存储类的对象并使用对象获取函数?
//in storage class
export class DataStorage{
constructor(type, value){
this._type = type;
this._value = value;
}
get type() {
return this._type;
}
//store objects of type storage class
import {DataStorage} from "./DataStorage.js";
export var aDS = [];
export function addDataStorage(type, value){
aDS.push(new DataStorage(type, value));
}
//use get function in storage class
import * as DS from "./AllDataStorage.js";
export function calculate(){
for(var i = 0; i < DS.aDS.length; i++){
console.log(DS.aDS[i].getType()); //Does not work
}
}
答案 0 :(得分:1)
你有一个getter功能,你不需要.getType()
你需要.type
。