Cloning a TypeScript Object

前端 未结 4 2362
眼角桃花
眼角桃花 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:15

    • Using standard ES6 features

      const clone = Object.assign({}, myObject)
      

      Warning: this performs a shallow clone.

      This excellent page from MDN contains tons of details on cloning, including a polyfill for ES5

    • A "quick" way of deep cloning is to use JSON utilities

      const clone = JSON.parse(JSON.stringify(myObject))
      
    • A "proper" way of cloning is to implement a clone method or a copy constructor...

    I know, I know, not enough JQuery

提交回复
热议问题