I have a game written in JavaScript that stores lots of game related data in objects. For example, the map of the world is written in a class like this:
const theMap = new GameMap();
class GameMap() {
constructor() {
this.rooms = [];
}
addRoom(name) {
this.rooms.push(name);
}
}
I want to store the variable theMap
in a string so I can write it to a file. I can use JSON.stringify(theMap)
to convert it to JSON, but it removes the methods. If I were to convert the JSON back to an object, I couldn't use addRoom
on it anymore. Is there a way to preserve those methods?