Reputation: 12005
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)
Upvotes: 0
Views: 957
Reputation: 8184
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);
Upvotes: 5