Can Javascript get a function as text? I'm thinking like the inverse of eval().
function derp() { a(); b(); c(); }
alert(derp.asString());
The result would be something like "a(); b(); c();"
Does it exist?
Can Javascript get a function as text? I'm thinking like the inverse of eval().
function derp() { a(); b(); c(); }
alert(derp.asString());
The result would be something like "a(); b(); c();"
Does it exist?
Updated to include caveats in the comments below from CMS, Tim Down, MooGoo:
The closest thing available to what you're after is calling .toString()
on a function to get the full function text, like this:
function derp() { a(); b(); c(); }
alert(derp.toString()); //"function derp() { a(); b(); c(); }"
You can give it a try here, some caveats to be aware of though:
.toString()
on function is implementation-dependent (Spec here section 15.3.4.2)
(function() { x=5; 1+2+3; }).toString()
== function() { x=5; }
function derp() { a(); b(); c(); }
alert(derp.toString());