I’m making a game with a shop, and i want to make a copy of the list to keep the costs saved. i tried a standard listBak = list
, but that just makes a reference to it, and a solution that should work is listBak = JSON.parse(JSON.stringify(list))
, however. list contains an object that contains itself causing an infinite loop and erroring out. here is an example of how the original list is being made
import { Item } from "item.js"
export class Obg{
list = []
constructor(main /* includes this class */){
this.list[0] = new Item("title", function buyFunc(){/* code executed when bought */}, main)
}
}
the reason i need the shop item to have access to main is the fact that buyFunc() normally edits things that are outside of both the shop item, and the parent class. it can edit anything from any file, and i need that versatility, i would prefer to not list out the specific areas it can access. i am also using vanilla javascript, I’m using GitHub pages so i can’t use anything that requires a server
i have tried;
listBak = list
: creates a link to list
listBak = JSON.parse(JSON.stringify(list))
: errors out due to circular reference
listBak = JSON.stringify(JSON.decycle(list))
: does not preserve functions in the object; using cycle.js from here