可能重复:
返回 Javascript 文件中定义的所有函数
好的,所以我有一个 JS 文件,其中定义了许多 JavaScript 函数。有没有办法使用 JavaScript 解析该特定 JS 文件中所有函数的名称。任何指导或链接来实现这一点将不胜感激
可能重复:
返回 Javascript 文件中定义的所有函数
好的,所以我有一个 JS 文件,其中定义了许多 JavaScript 函数。有没有办法使用 JavaScript 解析该特定 JS 文件中所有函数的名称。任何指导或链接来实现这一点将不胜感激
If it's just for development purposes then use an IDE. The IDE will depend on your environment. For example, you might you IntelliJ if your server side code is Java or Visual Studio's if you are a .NET shop.
If you really need to use javascript to dynamically go through the list of functions I suggest rethinking why you need to do it. If it turns out usefull you could namespace your functions and then just iterate over the namespace functions. See this answer for how to namespace https://stackoverflow.com/a/5947280/695461. Then iterate over the "public" functions.
Again, if it's just for development ease of use, use an IDE. They have whole teams of people writing parsers and syntax highlighters and structure diagrams for you.
If you are not generating the js file dynamically, the easiest and safest option is to keep a hard-coded list of function names.
All other parsing methods are risky because
looping through all window
objects for objects with typeof window[x] === 'function'