簡體   English   中英

JavaScript ecma6 中的對象引用

[英]Object references in JavaScript ecma6

如何將類的對象存儲在數組中並使用對象獲取函數?

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