简体   繁体   English

如何获得功能的“孩子”或内容?

[英]How do I get the “children” or contents of a function?

For example, if my script is... 例如,如果我的脚本是...

function cool() {
    function yes() { alert('yes'); }
    function wow() { alert('wow'); }
}

And I use cool.toString(); 我使用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 使用toString来获取整个函数,就像您在示例中所做的一样
  2. Replace the beginning of the toStringed-function, all the way until (and including) the first { with an empty string. 用空字符串替换toStringed函数的开头,一直到(包括第一个) {为止。
  3. Remove the last character of the toStringed-function. 删除toStringed函数的最后一个字符。

Like this: 像这样:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM