I want to pass an optional data parameter to some callbacks, but only to callbacks that support a single parameter; right now, I have a moderately-sized code base of callbacks that cannot accept a parameter at all. How can I check what parameters a Function
object supports?
Asked
Active
Viewed 2,195 times
12

Chris R
- 17,546
- 23
- 105
- 172
-
To be honest, the design smells a little funny. Can you post some code showing what you're trying to do? Because to the best of my knowledge, I don't believe there's any way to inspect a function from without to determine the number of parameters it defines in either JavaScript or ActionScript (other than as Patrick describes for ActionScript Class members), as this is typically known in advance by the developer. – Christian Nunciato Mar 03 '10 at 22:12
-
1Chris? Did you ever find an answer to this question? If so, would you mind posting an update? – Joshua Oct 04 '10 at 15:46
3 Answers
20
Function is an Object. Every function has a read-only property named length that stores the number of parameters defined for the function. Use it.

Lex
- 1,378
- 11
- 10
2
If your function is declared in a class
use the function
describeType it will return an XML you can parse and look at your function name with his arguments

Patrick
- 15,702
- 1
- 39
- 39
-
It is not; this is a lambda function, passed in as a callback. I can't seem to figure out how to get more information out of it. – Chris R Mar 03 '10 at 20:37
1
The arguments array is an array of all the parameters passed into a function. Maybe that is what you are looking for?
function traceArgArray(x:int):void
{
for (var i:uint = 0; i < arguments.length; i++)
{
trace(arguments[i]);
}
}
Example taken from livedocs.adobe.com

Robusto
- 31,447
- 8
- 56
- 77
-
That's not really all that helpful. I know what the arguments are from within the function, I'm looking at how to find out what they are from _outside_ the function's scope. – Chris R Mar 03 '10 at 20:37