gpt4 book ai didi

javascript - JavaScript ecma6 中的对象引用

转载 作者:行者123 更新时间:2023-11-27 23:05:02 24 4
gpt4 key购买 nike

如何将类的对象存储在数组中并使用对象获取函数?

//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
}
}

最佳答案

你有一个 getter 函数,你不需要 .getType() 你需要 .type

关于javascript - JavaScript ecma6 中的对象引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36676439/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com