MV Cloning an object (without references)

I initially started this thread to ask how this is possible, since it was driving me crazy, but I found a solution so I'm now writing it to share it.

Basically, when wanting to create a copy of an object and want to seclude it from its origin, to use it for tests or whatever, it can't be done by directly assigning the origin to the target.

So this:
JavaScript:
target = origin;
target.prop++
will always also change the value of origin.prop.

I tried doing this in about a billion ways. The solution is quite simple and is called "deep clone using JSON"

JavaScript:
target = JSON.parse(JSON.stringify(origin))

This seems to copy everything over to the target object, and remove all references between them.

I know this might be common knowledge, but it wasn't to me, so I'm sharing it for all that might be like me ^_^
I hope this wasn't supposed to be easy... Cause I still do not understand any of it. 😛

Glad to see you solved it yourself even before you were ready to ask for help. 🙂
To elaborate: this is because objects are passed by reference. Defining an object creates its data in memory, and the variable simply acts as a pointer to that memory location. If you assign it to another variable, that will make both variables point to the same data.

A deep clone, like you've described, makes all properties independent of their original source. It makes independent clones of all properties, sub-properties, etc. RMMV's core scripts define a method for deep clones (rpg_core.js):
JavaScript:
JsonEx.makeDeepCopy = function(object) {
    return this.parse(this.stringify(object));
};
By contrast, a shallow clone copies only the top-level properties. For example, if you were to make a shallow clone of this object:
JavaScript:
{
  a: 1,
  b: { c: 'cake' }
}
...then the b property of the clone will still refer to the same object as it does in the original ({ c: 'cake' }), since it has only copied the value (memory pointer) of that property. You can perform a shallow clone in JavaScript using Object.assign:
(That article also mentions the stringify/parse approach for deep-cloning.)

Deep and shallow clones can both be useful in different situations.
To elaborate: this is because objects are passed by reference. Defining an object creates its data in memory, and the variable simply acts as a pointer to that memory location. If you assign it to another variable, that will make both variables point to the same data.

A deep clone, like you've described, makes all properties independent of their original source. It makes independent clones of all properties, sub-properties, etc. RMMV's core scripts define a method for deep clones (rpg_core.js):
JavaScript:
JsonEx.makeDeepCopy = function(object) {
    return this.parse(this.stringify(object));
};
By contrast, a shallow clone copies only the top-level properties. For example, if you were to make a shallow clone of this object:
JavaScript:
{
  a: 1,
  b: { c: 'cake' }
}
...then the b property of the clone will still refer to the same object as it does in the original ({ c: 'cake' }), since it has only copied the value (memory pointer) of that property. You can perform a shallow clone in JavaScript using Object.assign:
(That article also mentions the stringify/parse approach for deep-cloning.)

Deep and shallow clones can both be useful in different situations.

I didn't know core had this function xD
Still, I made my own - better to work with my own so that I can remember them too.
But yeah, the rest of what you say is the info I also found, I read all about Object.assign / shallow clones too, but from what I got, it doesn't do what I want it for.

EDIT: I started using the JsonEx function inside my own. Basically added a clone function to the Info_Object class, that returns that function with "this" argument. So like, telling it to return a clone of itself. Cool stuff!

Original thread: https://forums.rpgmakerweb.com/threads/cloning-an-object-without-references.143725/