If you're using ES6 versions of Node, you can check out a small package I wrote called JSOFF. It's the JavaScript Object-Function Format; a drop-in replacement for JSON that handles functions.
It's super tiny and simple, so Babeljs or Browserify may be your friends.
Install via: npm install jsoff
or yarn add jsoff
.
Here is the example how to create an object with functions:
const JSOFF = require('jsoff');
var obj = {
abc: 123,
def: function (a,b) { return a * 2 + b * 3; },
ghi: a => { return a * 2 },
jkl: (a,b) => { return ((d,e) => { return a*d + b*e })(2,4) }
};
var str = JSOFF.stringify(obj);
// str is now:
// '{"abc":123,"def":"function (a,b) { return a * 2 + b * 3; }","ghi":"a => { return a * 2 }","jkl":"(a,b) => { return ((d,e) => { return a*d + b*e })(2,4) }"}');
});
var clone = JSOFF.parse(str);
clone.def(10,5) // 35
clone.ghi(5) // 10
clone.jkl(10,20) // 100