how to shallow clone javascript class


class myClass {
constructor() {
this.str = "String"; //Defining a string
this.number = 3; //Defining a number
}
}

var classReference = new myClass(); //Making new instance of class
var myObjectToCopy;

function copyClass() {
myObjectToCopy = JSON.parse(JSON.stringify(classReference)); //Converting class to a string then turning that string into an object
}

copyClass(); //Calling function to copy the class

//myObjectToCopy is now a copy of classReference

www.codegrepper.com
www.stackoverflow.com