how to shallow clone javascript class

Solutions on MaxInterview for how to shallow clone javascript class by the best coders in the world

showing results for - "how to shallow clone javascript class"
Richie
02 Nov 2017
1class myClass {
2  constructor() {
3    this.str = "String"; //Defining a string
4    this.number = 3; //Defining a number
5  }
6}
7
8var classReference = new myClass(); //Making new instance of class
9var myObjectToCopy;
10
11function copyClass() {
12  myObjectToCopy = JSON.parse(JSON.stringify(classReference)); //Converting class to a string then turning that string into an object
13}
14
15copyClass(); //Calling function to copy the class
16
17//myObjectToCopy is now a copy of classReference