【问题标题】:Object references in JavaScript ecma6JavaScript ecma6 中的对象引用
【发布时间】:2016-04-17 12:24:36
【问题描述】:

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

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

【问题讨论】:

  • 这与 ES6 无关。

标签: javascript arrays class get


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2017-06-02
    • 2010-10-05
    • 2015-09-09
    • 1970-01-01
    • 2016-04-08
    • 2016-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多