MZ Detach a copy from the original?

Not sure if this is a "I don't understand Javascript enough" problem or a "I don't fully understand how MZ handles these things" problem. I'm wanting to copy a database object, then have the new object have absolutely no connection to the original object, so that changes to the copy aren't made to the original as well(this kind of thing is both a blessing and a curse compared to VisualBasic. I'm so used to a different kind of programming language).
Basically I'm attempting to make unique versions of database equipment on the fly, and each item in the database is the base that I'm starting with.

I THOUGHT I had succeeded using
JavaScript:
newItem = Object.assign({}, item)
and afterward changing the id and pushing the new version to the $dataWeapons object(quick and dirty, probably, but I'm trying to get the thing running first), I was able to modify the name and have unique entries in the item screen and everything. Then I tried to modify the new item's stats, specifically params for the first test. Apparently there is still some connection SOMEWHERE to the original database object the new one spawned from, because any change to param on the new item is made to the original as well, and thus any copy made from that base afterward, causing ALL versions, base and new, to have the same params.

I know that pretty much any identifier on an item refers to the $data object, which is why I changed the copy's id and pushed the new version. But the only identifiers I can find on the object that seem to point to it's $data is the id, as well as the existence of wtypeId or atypeId to identify it as a weapon or armor. For reference, this copy is spawned from the item object in the Game_Party gainItem command. And up to this point, my changes seemed to have made the copy a completely independent item. So... what am I missing?

Thanks in advance!
I can tell you that JsonEx.makeDeepCopy(objectName) is the function you are looking for.

However, I'd question your intention to put the new item in $dataWeapons.

$dataWeapons is a static database that is loaded when you first launch your game and is not reset when you choose New Game or saved when you choose Saved Game, or reloaded/reset when you choose New Game. So if you are allowing your player to make new items as they go, as soon as you add the new weapon to $dataWeapons, if the player returns to the menu and chooses New Game, that new weapon will still be in the database (though they'd have no way to access it, of course). But then if they close down the game completely and relaunch another day to continue, when they load their save file that weapon will no longer exist in $dataWeapons.

So you will either need to include $dataWeapons in the save file, which means mods to the save and load functions, or you will need another way to save these new things you're creating. To be honest, adding $dataWeapons to the save file is probably the easiest approach.
I can tell you that JsonEx.makeDeepCopy(objectName) is the function you are looking for.

However, I'd question your intention to put the new item in $dataWeapons.

$dataWeapons is a static database that is loaded when you first launch your game and is not reset when you choose New Game or saved when you choose Saved Game, or reloaded/reset when you choose New Game. So if you are allowing your player to make new items as they go, as soon as you add the new weapon to $dataWeapons, if the player returns to the menu and chooses New Game, that new weapon will still be in the database (though they'd have no way to access it, of course). But then if they close down the game completely and relaunch another day to continue, when they load their save file that weapon will no longer exist in $dataWeapons.

So you will either need to include $dataWeapons in the save file, which means mods to the save and load functions, or you will need another way to save these new things you're creating. To be honest, adding $dataWeapons to the save file is probably the easiest approach.
Well, as I said, it's quick and dirty. I originally didn't want it in the $data at all, but I ran into issues getting the new items to appear in the inventory as unique items, because the function used to get the amount of a given item you have looks for the item to exist in a $data object and I don't particularly want to have to rewrite the ENTIRE inventory system to get both modified and unmodified/default items to work together, especially since I barely understand some parts of it as it is, though repeatedly accidentally breaking it trying to get this working has taught me quite a bit.

I'm already aware of the saving problems, and have already been thinking on the best way to solve the problem. The easiest would probably be to have the original $data lengths stored before modifying, so that when the player returns to the title, the object can be reset to default by simply getting rid of the extras, and then on loading, have it check all items with id numbers higher than the original lengths, and add them as necessary. But right now I'm more concerned with getting the functionality working properly before tackling the saving/loading issues.

On that note, what is the difference between the deep copy and the object.assign? As far as I'm aware, object.assign takes all properties of an object and copies them to another, which as far as I'm aware should have been all I needed once the id was changed on the copy.
The Object.assign() method copies all enumerable own properties from one or more source objects to a target object. It returns the target object.

Maybe not all properties are enumerable or own properties.
On that note, what is the difference between the deep copy and the object.assign?

Object.assign() does not copy arrays or other objects attached to the one you are trying to duplicate.

That's why it's called a "shallow" copy. It doesn't replicate "deeper" than the surface layer so to speak.

You'll notice that properties of your copied weapon like "price" and "description" are not linked between your shallow copy and the original. But the properties like "params" and "traits" which are Arrays are linked, because what the function did was "assign" your shallow copy the exact same pointer to those same Arrays that the original had.

It did not copy or make any new identical Arrays for the copied weapon.

Same goes for sub-objects too. The "meta" property is an Object rather than Array and it doesn't copy that it either, just links to the same Object the original had (you can test it, if you try and insert something into your copy's "meta" property, it will appear in the original's too).

"makeDeepCopy()" is called a "deep" copy because it DOES replicate other objects/arrays further down than the one on the surface you are trying to duplicate.
Object.assign() does not copy arrays or other objects attached to the one you are trying to duplicate.

That's why it's called a "shallow" copy. It doesn't replicate "deeper" than the surface layer so to speak.

You'll notice that properties of your copied weapon like "price" and "description" are not linked between your shallow copy and the original. But the properties like "params" and "traits" which are Arrays are linked, because what the function did was "assign" your shallow copy the exact same pointer to those same Arrays that the original had.

It did not copy or make any new identical Arrays for the copied weapon.

Same goes for sub-objects too. The "meta" property is an Object rather than Array and it doesn't copy that it either, just links to the same Object the original had (you can test it, if you try and insert something into your copy's "meta" property, it will appear in the original's too).

"makeDeepCopy()" is called a "deep" copy because it DOES replicate other objects/arrays further down than the one on the surface you are trying to duplicate.
Thank you so much for that explanation. I had noticed shallow and deep copy types when trying to find out how to make a copy, but the place I saw that assumes you already know what shallow and deep mean. I didn't know that those terms would be my problem, or that the copy's parameters were only pointers, which is why I assumed that Object.assign() was all I needed. I'm still learning javascript on the fly as I do this, which means I can figure out in general terms how to do what I need to do, but that on its own doesn't tell me WHY or HOW some things work, especially if it's related to something inherent to how the language operates in the first place, like prototype. I thought it was just something MZ's programmers did to keep their stuff straight. Only recently found out it was literally how javascript works.

In any case, the deepcopy did exactly what I wanted. Thanks Shaz for the function, and Traverse for the explanation!

This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

Original thread: https://forums.rpgmakerweb.com/threads/detach-a-copy-from-the-original.135035/