2008-11-04 7 views

답변

0

답변을 찾았습니다. 아래에 붙여 넣습니다.

@fenomas : 네, 맞습니다. 함수는 단지 객체이며, 내가 찾고있는 것은 그 객체에 대한 참조의 이름입니다 (존재하는 경우, 즉 함수가 익명이 아닌 경우). 또한 일반적으로 프로그래밍을하는 가장 좋은 방법은 아닌 것처럼 보입니다. 그러나 내 시나리오는 특별합니다. 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에만 해당.

관련 문제