There is a way to serialize a function in JS, but you'll have to eval it on the other side and it will also lose access to it's original scope. A way to do it would be:
JSON.stringify(objWithFunction, function(key, val) {
if (typeof val === 'function') {
return val + ''; // implicitly `toString` it
}
return val;
});
There are some legitimate uses for what you're asking despite what people are posting here, however, it all depends on what you're going to be using this for. There may be a better way of going about whatever it is you're trying to do.
Answer from chjj on Stack OverflowJSON.stringify function
Json stringify
typescript-json, 2x faster JSON stringify with only one line
JSON.stringify() is doing his job well. Thank you!
Videos
There is a way to serialize a function in JS, but you'll have to eval it on the other side and it will also lose access to it's original scope. A way to do it would be:
JSON.stringify(objWithFunction, function(key, val) {
if (typeof val === 'function') {
return val + ''; // implicitly `toString` it
}
return val;
});
There are some legitimate uses for what you're asking despite what people are posting here, however, it all depends on what you're going to be using this for. There may be a better way of going about whatever it is you're trying to do.
In fact, It's very easy to serealize / parse javascript object with methods.
Take a look at JSONfn plugin.
http://www.eslinstructor.net/jsonfn/