问题
I can already get at all of the functions in a class by doing something like the following.
for (var member in obj) {
if (obj[member] instanceof Function) {
var f:Function = obj[member];
...
}
}
Is there a way to get at a function's parameter list in actionscript? For example, can I write a function that does something like this?
function getFunctionArguments (f:Function) : Array {
var argumentArray:Array = new Array();
for (...) {
...
argumentArray.push({ name:<argumentName>, type:<argument type> });
}
return argumentArray;
}
If so, what do I fill in at the ellipses?
回答1:
Nosirree. I'd like to give you a workaround, but there's no way to introspect a function's signature like this.
What you can do is, when the function is actually called, inside it you can browse through the arguments irrespective of the signature, by looking into the arguments
object. As in:
function doSomething() {
if (arguments.length > 0) {
if (typeof arguments[0] == "string") {
....
}
}
}
and so on. But even then there's no way to find out the name of the arguments in the function signature (and this works fine even if there are no arguments in the signature, as above).
来源:https://stackoverflow.com/questions/972973/procedurally-accessing-a-functions-parameter-list-in-actionscript-2-0