从JavaScript中的堆栈跟踪中提取函数引用

时间:2019-01-04 13:28:12

标签: javascript node.js

我编写了一个函数,该函数使用$variables = [ 'number' => ['44234200234','44234242002'], 'from' => 'world', 'content' => 'I love to code', ]; (在node.js中)上的字符串处理从堆栈跟踪中提取项目:

new Error().stack

然后我提取函数名称。但是,该函数的名称不在全局范围的命名空间中:

function split_stack_string(stack_str) {
    let regexp = /^    at (\w*) \(([^\n]*):(\d+):(\d+)\)[^\n]*$/mg;
    let result;
    let stack_items = [];
    while (result = regexp.exec(stack_str)) {
        let [fullmatch, funcname, source_file, posx, posy] = result;
        delete fullmatch;
        stack_items.push({funcname, source_file, pos:[posx, posy]});
    }
    return stack_items;
}

但是,此解决方案还不够好,因为我大量使用了lambda函数,并且这些函数无法通过其名称使用(或者不是全局函数)。

有什么方法可以直接获取对堆栈中函数的引用?

0 个答案:

没有答案