I am creating a game with many objects, these objects all have their own functions that get called. I have one object that does not do anything and is just for cloning, when this object is cloned everything but the functions get cloned. the x-y gets cloned as well as other this like weight and strength. but the functions don't get cloned. The method I use for cloning is this.
objects[1] = JSON.parse(JSON.stringify(objects[0]))
This is not the only clone method I have tried, but all of them gave me the same result. I expect this to clone the object from objects[0] to objects[1] with everything including functions. (they are in an array so I can execute their functions). But only everything but functions are cloned.
Here is an example I made in node.js. (I don't have the anything solid in code as I like to test if it will work before putting it all together).
var original = {}; original.a = true; original.b = null; original.c = 82; original.d = "a string LOL"; original.e = function() { console.log("everything does not work?"); } original.f = [0, "a", true, false]; console.log(original) console.log(JSON.stringify(original)); console.log(JSON.stringify(JSON.parse(JSON.stringify(original))));