I have read how to clone objects in javascript and I know how to copy properties from the object. But I not only want to copy object's properties but I also want the copied object to have all the methods as the parent object had and I am not able to figure out how:
Here is what's happening:
var obj = {
'firstname' : "Harsh", // <<< Parent object
'lastnmae' : "singh",
introduce() {
console.log("Hello I am " + this.firstname);
}
}
var objCopy = JSON.parse(JSON.stringify(obj)); // deep copying the parent object
obj.introduce(); // << works fine when called on the parent object
objCopy.introduce(); // << does not work
// but I thought objCopy is the copy of obj ???
EDIT: I don't want shallow copying , I want a deep copy of the object. Most methods that I have come across copy only properties and not functions. I have gone through other articles but haven't found any answers.