Procedurally accessing a function's parameter list in actionscript 2.0

不想你离开。 提交于 2020-01-24 17:23:05

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!