0

I have instance of class:

let i = new I();

before adding to array I need clone it, create a copy:

arr.push(i);

I have tried:

Object.assign({}, i)
POV
  • 11,293
  • 34
  • 107
  • 201

1 Answers1

5

You can try the following code, it should work.

 let i = new I();
 //const clone = Object.assign({}, i);
 //arr.push(clone);
 //const clone = JSON.parse(JSON.stringify(i));
 //arr.push(clone);
 const clone = Object.create(
      Object.getPrototypeOf(i), 
      Object.getOwnPropertyDescriptors(i) 
  );
 arr.push(clone);
Bellash
  • 7,560
  • 6
  • 53
  • 86