I have a JavaScript file with many methods defined in it. Is there any way to know how many methods are in that file & what are the names of methods?
-
1may be this helps: http://stackoverflow.com/questions/6588977/how-to-to-extract-a-javascript-function-from-a-javascript-file – Waqas Aug 03 '11 at 03:56
4 Answers
Is there any way to know that how many methods are there in Java Script & what is the names of method?
Read the source or the documentation (if there is any).
If you're looking for some kind of "list avaialble methods" function, there isn't one. Writing one would be the equivalent of writing a javascript parser and perhaps even compiler.
Good luck with that. :-)

- 142,382
- 31
- 172
- 209
While, in theory, you could try to find everything that matched the pattern:
function <name>(
You'd be missing out on a lot of other types of functions. See, Functions are really just objects in JavaScript. They can be assigned, shared, modified, and moved around. So, you'd also have to find these:
var <name> = function() {}
And these:
function returnFunc() {
return function() {...}
}
var <name> = returnFunc();
As well as these:
obj.member = new Function();
And a nearly infinite variety of similar function definitions.
So, the answer is, most likely you can't. Unless the code is extremely narrowly constructed.

- 39,252
- 15
- 98
- 100
The short answer is "no".
The long answer is that if the JS file is your JS file, i.e., you control the content, then there are several ways that you can structure the code that will let you obtain a count or list of function names. Obviously that won't help you with other people's code. Apologies if you already know all of this, but just in case you don't: it's generally a good idea to wrap all of your "library" functions up as properties of a single object, something like this:
var myFunctionLibrary = {
doSomething : function() {},
somethingElse : function() {},
nonFunctionProperty : "test",
// etc.
}
This creates a single global variable called myFunctionLibrary
, which is an object with properties that are references to functions. (Note: there are several other ways to achieve a similar effect, ways that I prefer over this way, but this seems simplest for purposes of this explanation.) You then access the functions by saying:
myFunctionLibrary.doSomething();
// or
myFunctionLibrary["doSomething"]();
Because all of your functions are then contained in a specific object you can iterate over them like any other object:
var funcCount = 0;
var propCount = 0;
for (fn in myFunctionLibrary) {
if (typeof myFunctionLibrary[fn] === "function"){
funcCount++;
alert("Function name: " + fn);
} else {
propCount++;
}
}
alert("There are " + funcCount + " functions available, and "
+ propcount + " other properties.");
The main advantage, though, is that you don't have to worry about your functions potentially having the same names as functions in some other library that you want to use.

- 147,572
- 30
- 200
- 241
-
Thanks for help.We are fetching function name in JS but i want to fetch these function name in my Java file.I mean i want some solution so that i can go traverse my JS File,find out function name & list out them in my Java file.Is there any way so that i link my JS File,its function name with Java? – Eshika Aug 03 '11 at 04:47
-
If you write all the functions you want accessible from Java with the same signature (e.g. global function declaration), then perhaps you can. Otherwise you use [jsDoc](http://code.google.com/p/jsdoc-toolkit/) style comments, but neither method is robust. – RobG Aug 03 '11 at 04:55
-
@Eshika - you might want to update your question then, to something like "How would my Java program process a JS file to extract function names?" - and add the "Java" tag. If you can narrow the scope to just function declarations but _not_ function expressions it should make it somewhat easier to to parse, though you may still have some problems with nested functions. – nnnnnn Aug 03 '11 at 05:15
-
I am able to call JS file from Java & run that function through Rhino.Is there any way so that i can retrieve information of that particular file like number of methods,methods name? – Eshika Aug 03 '11 at 11:13
Write a program to load your file into Rhino, and then inspect the dictionaries and see what you have.

- 97,814
- 39
- 186
- 310
-
Inspect the dictionaries mean to check for available methods but i could not find any appropriate way which can give me methods name in my JS File.Do you have any other idea or am i doing something wrong. – Eshika Sep 19 '11 at 07:06