我可以在javascript中将自定义数据类型数组存储在自定义对象中

时间:2017-07-15 18:04:54

标签: javascript arrays custom-object custom-data-type

我需要一个工作人员的自定义对象,所以我在staff.js中有这个:

    function Staff(name, shouldBeRemoved, officeName, dateHired) {
        this.name = name;
        this.shouldBeRemoved = shouldBeRemoved;
        this.officeName = officeName;
        this.dateHired = dateHired;
    }

我还在office.js中有一个Office的自定义对象:

    function Office(name, shouldBeRemoved, staffMembers) {
        this.name = name;
        this.shouldBeRemoved = shouldBeRemoved;
        this.staffMembers = staffMembers;
    }

我想将staffMembers属性用作Office中的Staff对象数组。

但是当我尝试将新的staff对象推送到Office对象中的staffMembers数组时,没有任何反应:

staff = new Staff(name, false, officeSelected, d.toDateString());
        for (i=0; i<officeArray.length; i++) {
            if (officeArray[i].name == officeSelected) {
                officeArray[i].staffMembers.push(staff);
            }
        }

1 个答案:

答案 0 :(得分:0)

我的建议是开始使用Typescript,原因是数据类型和类具有清晰的图像。

 staff = new Staff(name, false, officeSelected, d.toDateString());
    for (i=0; i<officeArray.length; i++) {
        if (officeArray[i].name == officeSelected) {
            officeArray[i].staffMembers.push(staff);
        }
    }

这里你应该检查officeArray [i] .name === staff.officeSelected