简体   繁体   English

JavaScript:如何提取JavaScript代码中所有函数的源代码?

[英]JavaScript: How to extract source code of all functions in a JavaScript code?

I would like to have a list of functions of a JS code together with the sources for each function. 我想要一个JS代码的功能列表以及每个功能的来源。 I'm completely new in this area, I found that I need to build AST of the code and traverse it. 我是这个领域的新手,我发现我需要构建代码的AST并遍历它。 By searching on the web I found this API, but it only gives me a list of function names, how can I extract the source code of functions? 通过在网上搜索,我找到了 API,但它仅提供了一个函数名列表,如何提取函数的源代码?

You can make use of toString() to convert the body of function into string. 您可以使用toString()将函数体转换为字符串。 Try this 尝试这个

function someFunction(){
    var crazyVariable = 10.123123123;
}

someFunction.toString();

it will return you this "function someFunction(){ var crazyVariable = 10.123123123; }" 它将返回此"function someFunction(){ var crazyVariable = 10.123123123; }"

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

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