这是对象。 TypeModel对象和StatusModel对象有一个属性typeID
,必须是一个数字(例如1)。
export class TypeModel{
typeID: number;
typeDescription: string;
}
export class StatusModel{
typeId: number;
statusId: number;
typeStatusDescr: string;
}
export class TypeStatusesModel{
type: TypeModel;
status: StatusModel;
}
let typeStatuses: TypeStatusesModel[] = [];
let typeStatusFirst: TypeStatusesModel= new TypeStatusesModel();
typeStatusFirst = {
type: {
typeID:1,
typeDescription: "Description Type 1"
},
status: {
typeId: 1,
statusId: 1,
typeStatusDescr: "Description Status 1"
}
}
typeStatuses.push(typeStatusFirst);
let typeStatusSecond: TypeStatusesModel= new TypeStatusesModel();
typeStatusSecond= {
type: {
typeID:2,
typeDescription: "Description Type 2"
},
status: {
typeId: 1;
statusId: 2;
typeStatusDescr: "Description Status 2";
}
}
typeStatuses.push(typeStatusSecond);
问题是当我尝试将typeStatuses数组中第二个对象的属性typeID
更改为数字2
时,我在第一个对象中更改了相同的属性(typeID
)。我尝试创建一个临时对象并替换掉#34;破坏的对象"但没什么。(let tempObject = Object.create(typeStatuses[1])
)
答案 0 :(得分:0)
你有一些错误的分号应该只是昏迷。我也会改变那些仅用于接口的条款,因为它们只是在没有任何行为的情况下声明属性。
export class TypeModel{
typeID: number;
typeDescription: string;
}
export class StatusModel{
typeId: number;
statusId: number;
typeStatusDescr: string;
}
export class TypeStatusesModel{
type: TypeModel;
status: StatusModel;
}
let typeStatuses: TypeStatusesModel[] = [];
let typeStatusFirst: TypeStatusesModel= new TypeStatusesModel();
typeStatusFirst = {
type: {
typeID:1,
typeDescription: "Description Type 1"
},
status: {
typeId: 1,
statusId: 1,
typeStatusDescr: "Description Status 1"
}
}
typeStatuses.push(typeStatusFirst);
let typeStatusSecond: TypeStatusesModel= new TypeStatusesModel();
typeStatusSecond= {
type: {
typeID:2,
typeDescription: "Description Type 2"
},
status: {
typeId: 1,
statusId: 2,
typeStatusDescr: "Description Status 2",
}
}
typeStatuses.push(typeStatusSecond);
typeStatuses[0].type.typeID=234;
console.log(JSON.stringify(typeStatuses));
它适用于控制台日志
[{" type":{" typeID":234," typeDescription":"描述类型 1"}," status":{" typeId":1," statusId":1," typeStatusDescr":&# 34;描述 状态1"}},{"类型":{" typeID":2," typeDescription":"描述类型 2"}," status":{" typeId":1," statusId":2," typeS tatusDescr":&# 34;描述 状态2"}}]
所以代码应该是这样的。我也删除了导出,因为我无法看到你在外部使用接口。
interface TypeModel{
typeID: number;
typeDescription: string;
}
interface StatusModel{
typeId: number;
statusId: number;
typeStatusDescr: string;
}
interface TypeStatusesModel{
type: TypeModel;
status: StatusModel;
}
let typeStatuses: TypeStatusesModel[] = [];
let typeStatusFirst: TypeStatusesModel;
typeStatusFirst = {
type: {
typeID:1,
typeDescription: "Description Type 1"
},
status: {
typeId: 1,
statusId: 1,
typeStatusDescr: "Description Status 1"
}
}
typeStatuses.push(typeStatusFirst);
let typeStatusSecond: TypeStatusesModel;
typeStatusSecond= {
type: {
typeID:2,
typeDescription: "Description Type 2"
},
status: {
typeId: 1,
statusId: 2,
typeStatusDescr: "Description Status 2",
}
}
typeStatuses.push(typeStatusSecond);
typeStatuses[0].type.typeID=234;
console.log(JSON.stringify(typeStatuses));