Functions already have a toString() method ... so just (function() {}).toString() should work.
For example, in node REPL:
> (function() { console.log("hello"); }).toString() 'function () { console.log("hello"); }'
Here is a link to some documentation .
source share