can an actionscript function find out its own name?

前端 未结 7 1028
借酒劲吻你
借酒劲吻你 2020-12-16 01:06

given the following

function A(b:Function)   { }

If function A(), can we determine the name of the function being passed in as parameter \'

7条回答
  •  青春惊慌失措
    2020-12-16 01:32

    I've been trying out the suggested solutions, but I ran into trouble with all of them at certain points. Mostly because of the limitations to either fixed or dynamic members. I've done some work and combined both approaches. Mind you, it works only for publicly visible members - in all other cases null is returned.

        /**
         * Returns the name of a function. The function must be publicly visible,
         * otherwise nothing will be found and null returned.
    Namespaces like * internal, protected, private, etc. cannot * be accessed by this method. * * @param f The function you want to get the name of. * * @return The name of the function or null if no match was found.
    * In that case it is likely that the function is declared * in the private namespace. **/ public static function getFunctionName(f:Function):String { // get the object that contains the function (this of f) var t:Object = getSavedThis(f); // get all methods contained var methods:XMLList = describeType(t)..method.@name; for each (var m:String in methods) { // return the method name if the thisObject of f (t) // has a property by that name // that is not null (null = doesn't exist) and // is strictly equal to the function we search the name of if (t.hasOwnProperty(m) && t[m] != null && t[m] === f) return m; } // if we arrive here, we haven't found anything... // maybe the function is declared in the private namespace? return null; }

提交回复
热议问题