Categories
JavaScript Answers

How to clone a JavaScript ES6 class instance?

Spread the love

To clone a JavaScript ES6 class instance, we use a few object methods.

For instance, we write

const clone = Object.assign(Object.create(Object.getPrototypeOf(orig)), orig);

to get the prototype of the orig object with Object.getPrototypeOf.

Then we create a new object from the prototype with Object.create.

Then we call Object.assign to make a copy of the object we created and return it.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *