Cloning a TypeScript Object

前端 未结 4 2363
眼角桃花
眼角桃花 2021-02-20 05:58

I have a typescript class

export class Restaurant {

  constructor ( private id: string, private name: string ) {

  }

  public getId() : string {
    return th         


        
4条回答
  •  伪装坚强ぢ
    2021-02-20 06:09

    If you're using TS 2.1, you can use object spread operator to create a shallow copy:

    const obj = { a: 1 };
    const clonedObj = { ...obj };
    

提交回复
热议问题