I want to store objects (with methods) in to file or DB. For example
var fs = require('fs');
function Worker(name, age){
this.name = name;
this.age = age;
}
Worker.prototype.work = function(){
console.log("I am working...");
}
worker = new Worker("Alice", 20);
worker.work();
fs.writeFile(
"worker.json",
JSON.stringify(worker),
function(){}
);
When I read the JSON file I can load the "work()" method:
fs.readFile("worker.json", function(err, data) {
worker = JSON.parse(data);
worker.work(); // I am working...
}
How can I achieve this?