shallow copy of an object javascript code example

Example 1: js shallow copy

Object.assign({}, obj); // ES6 shallow copy

Example 2: 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