One of the things that always bothered me about the original CivClicker was that the save and load methods were huge. I had to construct everything into a single mass to save it, and then load in each individual variable when loading the game. This isn't even remotely practical for CivClicker II, which will end up having vastly more data to save.

I've decided to take advantage of something I couldn't use last time (due to not using jQuery) and use the extend() method, which merges two objects. This actually makes things very very simple in one respect, provided you remember how it works.

http://api.jquery.com/jquery.extend/

Using extend, you can save one copy of your game's data structure, and then when loading use it to overwrite the default copy, altering values as well as recursively adding objects and properties.

Ultimately this is very simple, code wise, provided you have your data in JSON format:

$.extend(true, defaultData, loadedData);

The defaultData is your default (new game) data structure. It typically contains values set to zero or some other default number. It can contain objects which themselves contain values, or even more objects which contain yet more values, etc. - effectively, a tree structure of data.

(NB. Don't make the same mistake I made - only include serialisable data in your data structure if you want to be able to save to localStorage or a cookie - serialising strips out methods and doesn't work so well with self-referential data)

The loadedData parameter is your save data after it's been stringified using JSON.stringify(), saved, loaded, and turned back into an object with JSON.parse(). This is the version of your data structure that you want to be canonical - values here will overwrite values in your defaultData.

The first parameter (the one we set to true) is an option to make the merge process recursive (i.e. it delves into objects that are children of the objects, and their children, and... you get the idea).

The result? Your load function only needs to contain the things necessary for the extend() function to do its job, rather than having a massive series of properties that it must iterate through, or some finicky and easily-broken iteration system.

There are downsides, of course - you have to pay attention to how you construct your data structure to allow this pattern to work. You also have to have sensible defaults - because this is a recursive extension rather than a complete overwriting of the original data (you want to do this so that you can add more stuff to your game over time) it's harder to pick and choose which things you want to load in and what you want to overwrite.