How do I get the "children" or contents of a function?
For example, if my script is...
function cool() {
function yes() { alert('yes'); }
functio开发者_StackOverflow社区n wow() { alert('wow'); }
}
And I use cool.toString();
then I get the entire function as a string.
But what do I have to do to just get the inner-contents as a string:
function yes() { alert('yes'); }
function wow() { alert('wow'); }
- Use
toString
to get the entire function, like just did in your example - Replace the beginning of the toStringed-function, all the way until (and including) the first
{
with an empty string. - Remove the last character of the toStringed-function.
Like this:
cool.toString().replace(/^[^{]*{/, "").slice(0, -1)
精彩评论