I am trying to save an object in redis, which is an instance of a class and therefore has functions, here is an example:
function myClass(){ this._attr = "foo"; this.getAttr = function(){ return this._attr; } }
Is there a way to store this object in redis along with functions? I tried JSON.stringify() , but only properties are saved. How to store function definitions and perform the following actions:
var myObj = new myClass(); var stringObj = JSON.stringify(myObj); // store in redis and retreive as stringObj again var parsedObj = JSON.parse(stringObj); console.log(myObj.getAttr()); //prints foo console.log(parsedObj.getAttr()); // prints "Object has no method 'getAttr'"
How can I get foo when calling parsedObj.getAttr() ?
Thank you in advance!
EDIT
There was a suggestion to change MyClass.prototype and save the values, but what about something like this (functions other than setter / getter):
function myClass(){ this._attr = "foo"; this._accessCounts = 0; this.getAttr = function(){ this._accessCounts++; return this._attr; } this.getCount = function(){ return this._accessCounts; } }
I am trying to illustrate a function that calculates something like a count or average when it is called, among other things.
source share