简体   繁体   English

JavaScript ecma6 中的对象引用

[英]Object references in JavaScript ecma6

How to store objects of a class in an array and use the objects get function?如何将类的对象存储在数组中并使用对象获取函数?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM