代码之家  ›  专栏  ›  技术社区  ›  Ran

在actionscript2中有没有找到调用函数的名称的方法?

  •  1
  • Ran  · 技术社区  · 16 年前

    在ActionScript函数(方法)中,我可以访问arguments.caller,它返回一个函数对象,但我找不到这个函数对象所表示的函数的名称。 它的toString()只返回[函数],我找不到任何其他有用的访问器来提供… 帮助:

    3 回复  |  直到 16 年前
        1
  •  1
  •   fenomas    16 年前

    一个函数和其他任何函数一样只是一个对象——它本身没有一个“名称”;它只有一个名称,在这个意义上,你可以对它进行一个或多个引用。如果您要问的是如何获取调用函数的引用的名称,那么就没有一般的方法可以做到这一点。(毕竟,函数可以匿名声明,在这种情况下,它根本没有名称。)

    最好检查一下为什么您需要知道函数的名称,并找出其他方法来传递或访问您试图从该名称获得的信息。传递一个额外的参数可能是一种方法,但这取决于您正在做什么。

        2
  •  1
  •   Ran    16 年前

    我找到一个答案,然后把它贴在下面。

    @费诺马斯:是的,你是对的,当然,函数只是对象,我要找的是引用它们的名称(如果存在,也就是说,函数不是匿名的)。您也正确地指出,一般来说,这看起来不是进行编程的最佳方法;-)但是我的场景是特殊的:我希望使用check.checktrue和checks.checkref等方法实现一个checks类(类似于c check),在这种方法中,当检查失败时,我会得到一个很好的跟踪。跟踪将只出现在调试版本中,而不是发布版本中。

    我使用的是mtasc,下面的代码只适用于mtasc。它也应该只用于调试目的,而不是发布。 该技术是在全局上迭代,并找到一个等于调用函数的函数。这是一个不总是有效的黑客(匿名),但在大多数情况下,它对我很好。

    39:   /**
    40:    * Checks that cond is true. Use this method to validate that condition
    41:    * cond holds.
    42:    * If cond is false, traces a severe message and returns false to indicate
    43:    * check failure.
    44:    *
    45:    * @param cond the contition expected to be true
    46:    * @param msg the message to emit in case the condition is false.
    47:    *
    48:    * @return false is cond is false
    49:    */
    50:   public static function checkTrue(cond:Boolean, msg:String):Boolean {
    51:     if (!cond) {
    52:       trace("severe", "CHECK FAILED at " +
    53:           **getFunctionName(arguments.caller)** + ":\n" + msg);
    54:     }
    55:     return cond;
    56:   }
    
    
    94:   /**
    95:    * Gets the name of the function func.
    96:    * Warning: Use this only in debug version, not in release
    98:    *
    99:    * @return The full package path to the function. null if the function
    100:    *      isn't found.
    101:    */
    102:   private static function getFunctionName(func:Function):String {
    103:     var name:String = getFunctionNameRecursive(func, _global);
    108:     return name;
    109:   }
    110: 
    111:   /**
    112:    * Gets the name of the function func by recursively iterating over root.
    113:    * Warning: Use this only in debug version, not in release
    114:    */
    115:   private static function getFunctionNameRecursive(func:Function,
    116:       root:Object):String {
    117:     if (!root) {
    118:       return null;
    119:     }
    120: 
    121:     // Iterate over classes in this package
    122:     // A class is a function with a prototype object
    123:     for (var i:String in root) {
    124:       if (root[i] instanceof Function && root[i].prototype != null) {
    125:         // Found a class.
    126:         // Iterate over class static members to see if there's a match
    127:         for (var f:String in root[i]) {
    128:           if(root[i][f] == func) {
    129:             return i + "." + f;
    130:           }
    131:         }
    132:         // Loop over the class's prototype to look for instance methods
    133:         var instance:Object = root[i].prototype;
    134:         // Reveal prototype's methods.
    135:         // Warning: Not to be used in production code!!!
    136:         // The following line make all the instance attributes visible to the
    137:         // for-in construct. The "n" value is 8 which means "unhide"
    138:         // See http://osflash.org/flashcoders/undocumented/assetpropflags
    139:         // This operation is later undone by setting the "n" to 1 which means
    140:         // "hide"
    141:         _global.ASSetPropFlags(instance, null, 8, 1);
    142:         for (var f:String in instance) {
    143:           if(instance[f] == func) {
    144:             return i + "." + f;
    145:           }
    146:         }
    147:         // And hide instance methods again
    148:         // This line undoes the previous ASSetPropFlags
    149:         _global.ASSetPropFlags(instance, null, 1, false);
    150:       }
    151:     }
    152: 
    153:     // Iterate over sub packages. Sub packages have type "object"
    154:     for (var i:String in root) {
    155:       if (typeof(root[i]) == "object") {
    156:         var name:String = getFunctionNameRecursive(func, root[i]);
    157:         if (name) {
    158:           return i + "." + name;
    159:         }
    160:       }
    161:     }
    162:     return null;
    163:   }
    
        3
  •  0
  •   Alan    16 年前

    据我所知,不是在AS2 仅AS3。