0

Possible Duplicate:
Return all of the functions that are defined in a Javascript file

OK so i have a JS file in which i have a number of JavaScript functions defined.Is there any way to parse out the name of all function in that particular JS file using JavaScript.Any guidance or link to achieve this will be appreciated

Community
  • 1
  • 1
iJade
  • 23,144
  • 56
  • 154
  • 243

2 Answers2

0

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

  1. String parsing of the code is not safe, since you will have to cater too many cases
  2. There are options to get all global functions. But they are browser dependent. like looping through all window objects for objects with typeof window[x] === 'function'
Wolf
  • 2,150
  • 1
  • 15
  • 11
0

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.

Community
  • 1
  • 1
Daniel Moses
  • 5,872
  • 26
  • 39