I have objects which are 'stringify'd with JSON.stringify(), then saved with localstorage.setItem(), then are retrieved with localstorage.getItem(), then are parsed with JSON.parse(), and finally are returned into an array of objects used within the program. This is my code:
var exampleObjects = [];
function objectExample() {
this.exampleFunction = function() {
return this.otherObjectCreatedElsewhere.value;
}
this.otherObjectCreatedElsewhere;
}
function main() {
exampleObjects[ 0 ] = new objectExample();
exampleObjects[ 0 ].otherObjectCreatedElsewhere = otherObjectCreatedElsewhere;
exampleObjects[ 0 ].exampleFunction(); //Works
var save0 = JSON.stringify( exampleObjects[ 0 ] );
localstorage.setItem( 'key', save0 );
save0 = localstorage.getItem( 'key' );
exampleObjects[ 0 ] = JSON.parse( save0 );
exampleObjects[ 0 ].exampleFunction(); //No longer works, instead throws error exampleObjects[ 0 ].exampleFunction is not a function
}
main();
Now I've looked up JSON.parse with reviver methods, but I just cannot for the life of me figure it out. I have not gone to school for any of this, this is just a hobby of mine, but one that I've been cultivating for a couple years now. I really enjoy it, but times like these are frustrating.
EDIT
I have resolved this with the invaluable advice from cloudfeet. Basically I took the objects from the saved JSON string then parsed them into objects, then created a new object and reassigned all the rich properties.
Thanks again!
true,false, andnull, but you can't have a function.