JSON.stringify function

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.

Leave a Comment