greepow greepow - 3 months ago 9
Javascript Question

How to get function body text in JavaScript?

function derp() { a(); b(); c(); }


derp.toString()
will return
"function derp() { a(); b(); c(); }"
, but I only need the body of the function, so
"a(); b(); c();"
, because I can then evaluate the expression. Is it possible to do this in a cross-browser way?

Answer
var entire = derp.toString(); 
var body = entire.slice(entire.indexOf("{") + 1, entire.lastIndexOf("}"));

print(body); // "a(); b(); c();"

Please use the search, this is duplicate of this question