greepow greepow - 2 months ago 5x
Javascript Question

How to get function body text in JavaScript?

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

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?

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