2008-11-04 74 views
1

在一個actionscript函數(方法)中,我可以訪問arguments.caller,它返回一個Function對象,但是我找不到這個Function對象表示的函數的名字。它的toString()只是返回[Function],我找不到任何其他有用的訪問器給我,... 幫助: -/有沒有辦法在actionscript2中找到調用函數的名稱?

回答

0

我找到了答案,我會在下面粘貼它。 @finomas:是的,你當然是對的,函數只是對象,我要找的是對它們的引用(如果存在的話,即函數不是匿名的)的名稱。一般來說,這看起來並不是編程的最佳方式;-)但是,我的場景很特別:我想用Check.checkTrue和Checks等方法實現Checks類(類似於C CHECK)。 checkRef,當檢查失敗時,我得到一個很好的跟蹤。跟蹤將只在調試版本中出現,而不是在版本中。

我正在使用MTASC,下面的代碼只適用於MTASC。它也應該僅用於調試目的而不是釋放。 該技術是在_global上迭代並找到一個與我的調用函數相同的函數。這是一種黑客行爲,並不總是有效(匿名),但在大多數情況下,它對我來說很好。

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: } 
1

函數只是一個對象像任何其他 - 它不有一個「名」本身;它只有一個名稱,因爲您可以對其進行一次或多次引用。如果你所要問的是如何獲得函數被調用的引用的名稱,那麼沒有通用的方法來做到這一點。 (所有功能可以匿名聲明,在這種情況下,它沒有任何名稱。)

這可能是最好的,以檢查爲什麼你需要知道該函數的名稱,並找出一些其他方式來傳入或訪問您試圖從該名稱派生的信息。傳遞一個額外的參數可能是一種方法,但這取決於你在做什麼。

0

據我所知,不在AS2 只有AS3。

相關問題