In addition to "edge cases", jQuery.extend can be used as you mentioned. a
b
, and their clones will refer to the same object c
.
var c = { f:'see' }; var a = { fa: c }; var b = { fb: c }; var cloneA = $.extend({}, a); var cloneB = $.extend({}, b); console.log(a.fa === b.fb, cloneA.fa === cloneB.fb, a.fa === cloneB.fb);
But it looks like you want to clone all objects (including c
) by tracking the relationships of the objects. For this, it is best to use object relationship tables.
I see this a lot with nested javascript and JSON objects, because people tend to forget that JSON is a purely text format. There are no real javascript objects in the JSON file except one line of text instanceof String
. There is no beans or pickles or any conservatively heavy products in javascript.
In the table of object relations, each "table" is just an array of "flat" objects with only primitively evaluated properties and pointers (not links) to other objects in the table (or in another table). The pointer can only be the index of the target.
Thus, the JSON version of the above relation to the object may look something like this:
{ "table-1":[ { "a": { "fa":["table-2",0] } }, { "b": { "fb":["table-2",0] } } ], "table-2":[ { "c": { "name":"see" } }, { "d": { "name":"dee" } }, { "e": { "name":"eh.."} } ] }
And the parser might look like
var tables = JSON.parse(jsonString); for(var key in tables){ var table = tables[key]; for(var i = 0; i < table.length; i++){ var name = Object.keys(table[i]) var obj = table[i][name]; for(var key2 in obj){ if(obj[key2] instanceof Array && obj[key2][0] in tables){ var refTable = obj[key2][0]; var refID = obj[key2][1]; var refObj = tables[refTable][refID]; var refKey = Object.keys(refObj)[0]; obj[key2] = refObj[refKey]; } } this[name] = obj; } } console.log(a.fa === b.fb, b.fb === c);
I understand that comparing the relationship to an object has problems, but photographing the script engine sounds a little crazy. Especially since your intention is to be able to remember each previous step, because then you need a new snapshot for each step ... which will very quickly take the shit a ton of disk space .. if you just track the snapshot the difference between each step like a git repository. It sounds like a terrible job to implement a seemingly simple βundoβ method.
Damn it ... think about it, why not just store every step in the story file? Then, if you need to step back, just crop the history file in the previous step and run each step again in the new environment.
Not sure how practical it would be (in terms of performance) using java. Nodejs (as it exists today) is faster than any java script engine will ever be. In fact, I'm just going to call it ECMAscript from now on. Sorry to pounce, its just
Java is slow, but you already know.
Because it easily shows that as fast as it happens.