Created
May 9, 2020 15:06
-
-
Save Capital-EX/3b91fe818b128fa5a1f5677d935547de to your computer and use it in GitHub Desktop.
Implement the interface SugarCube uses for serializing and deserializing objects.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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