Skip to content

Instantly share code, notes, and snippets.

@Capital-EX
Created May 9, 2020 15:06
Show Gist options
  • Save Capital-EX/3b91fe818b128fa5a1f5677d935547de to your computer and use it in GitHub Desktop.
Save Capital-EX/3b91fe818b128fa5a1f5677d935547de to your computer and use it in GitHub Desktop.
Implement the interface SugarCube uses for serializing and deserializing objects.
window.SugarCubeObject = class SugarCubeObject {
constructor() {
this.reviveWrapper = '(new ' + this.constructor.name + '())._init($ReviveData$)'
}
_init(obj) {
Object.keys(obj).forEach(pn => {this[pn] = clone(obj[pn])});
return this;
}
clone () {
return (new this.constructor())._init(this);
}
toJSON () {
let ownData = {};
Object.keys(this).forEach(pn => {ownData[pn] = clone(this[pn])});
return JSON.reviveWrapper(this.reviveWrapper, ownData)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment