Open nyteshade opened 7 years ago
After a little research it may be sufficient to simply capture
JSONEx = {/* Your functions for JSONEx stuff */}
let instance = ...; // value to capture
let prototype = Reflect.getPrototypeOf(instance);
let capture = {
instance: JSONEx.stringify(instance),
prototype: JSONEx.stringify(prototype),
constructor: prototype.constructor.toString()
};
// Reconstruction
function reconstruct(capture) {
let newInstance = null;
(() => {
let _class = eval(`({capture.constructor})`);
let _inst = JSONEx.parse(capture.instance);
let _proto = JSONEx.parse(capture.prototype);
newInstance = Object.create(_inst);
Reflect.setPrototypeOf(newInstance, _proto);
})();
return newInstance;
}
Or something of the like.
thank you @nyteshade I will look into adding this functionality! Will update on any progress I make back to this thread.
This is awesome +1
Objects that are instances of any given type, typically class instances, should also serialize their constructor chain back to ECMA script types; i.e. Function. For example
Given the code above you have the lineage of a given class instance. In order to accurately rebuild this object in a different environment, you'll need to persist those classes (via toString()) and recreate them before recreating the object in question if the target environment does not already contain those object definitions.
Perhaps it'll end up being something like
Where when parsed, C, B, and A will be recreated if necessary. It is arguable as to whether these need to be created globally or within an iffy or something of that nature to protect scope creep. Then afterwards follow up with a
let instance = Object.create(C); Object.assign(instance, classInstance[0])
thereby creating a new instance of C with its prototype chain intact and then doing an Object.assign on top to copy the previous state of the object instance.Afterwards the following should work
I may be missing some details here; namely
Reflect.getPrototypeOf
returns the prototype, not the constructor. There may be some tweaking needed here.