开发者

How do I get the "children" or contents of a function?

开发者 https://www.devze.com 2023-04-08 21:25 出处:网络
For example, if my script is... function cool() { function yes() { alert(\'yes\'); } functio开发者_StackOverflow社区n wow() { alert(\'wow\'); }

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'); }


  1. Use toString to get the entire function, like just did in your example
  2. Replace the beginning of the toStringed-function, all the way until (and including) the first { with an empty string.
  3. Remove the last character of the toStringed-function.

Like this:

cool.toString().replace(/^[^{]*{/, "").slice(0, -1)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号