我如何得到“孩子们”?或者函数的内容

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

本文关键字:孩子们 或者 函数 何得      更新时间:2023-09-26

例如,如果我的脚本是…

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

我用cool.toString();然后我得到整个函数作为字符串

但是我要怎么做才能把内部内容变成字符串呢?

    function yes() { alert('yes'); }
    function wow() { alert('wow'); }
  1. 使用toString来获得整个函数,就像刚才在您的示例中所做的那样
  2. 用空字符串替换tostring -function的开头,直到(包括)第一个{
  3. 删除tostring -function的最后一个字符

:

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