Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript-对象分解-奇怪_Javascript_Angular_Destructuring - Fatal编程技术网

Javascript-对象分解-奇怪

Javascript-对象分解-奇怪,javascript,angular,destructuring,Javascript,Angular,Destructuring,这是指一个平均项目。在Angular中,讨论的两个模型是-服务和客户机 export class Service { _id?: string; servName: string; servCat: string; freq: string; fees: number; dueDay: number; dueMonth: number; } // This is same as the model in Node. export clas

这是指一个平均项目。在Angular中,讨论的两个模型是-服务和客户机

export class Service {
    _id?: string;
    servName: string;
    servCat: string;
    freq: string;
    fees: number;
    dueDay: number;
    dueMonth: number;
}

// This is same as the model in Node.
export class Client {
    compName: string;
    ....
    ....
    services?: [{
        servId: string,    // type ObjectId in Node, gets populated when data is fetched in Angular
        fees: number
    }];
}

// This model is used to handled the fetched client data which has populated services data
export class ClientWithProfile {
    compName: string;
    ....
    ....
    services?: Service[];
}

在Angular中,在编辑服务数组之后,当我试图保存客户端时

console.log('client', this.client)  // of Type ClientWithProfile, i.e. with full services array

//Destructuring client object
const { services, ...otherProps } = this.client

console.log('otherProps', otherProps)  // shows Client data without Services Array

// savingClient is of Type Client
this.savingClient = { ...otherProps }

console.log('savingClient', this.savingClient)
 ////?????? shows client data with Services array, with just servId and fees?????
 /// How is that possible???
我无法理解以下内容: 在分解
this.client
对象(它有完整的服务数组)之后,我得到一个常量
otherProps
,没有完整的服务数组

当我将该
otherProps
分配给另一个对象
savingClient
时,为什么只有ID和费用的服务数组包含在
savingClient
中?对象
savingClient
从未在代码中的此语句之前创建过


有人能解释一下吗?

破坏类是错误的,要么调用它们的接口并使用一个简单的对象,要么使用一些函数从一个对象导出另一个对象。。。太多的代码不清楚。如果从类型
ClientWithProfile
创建的
savingClient
没有
services
属性,那么
savingClient
如何成为类型
Client
呢?可能是不相关的,但
services?:[{servId:string,fees:number}]
看起来是错误的。你是说
服务吗?:{servId:string,fees:number}[]