문제

나는 물체의 각 구성원을 반복하려고 노력하고 있습니다. 각 멤버에 대해, 나는 그것이 함수인지 아닌지 확인합니다. 그것이 함수 인 경우, 그 이름을 얻고 함수의 이름에 따라 일부 논리를 수행하고 싶습니다. 그래도 이것이 가능하는지 모르겠습니다. 그게? 팁이 있습니까?

예시:

var mems: Object = getMemberNames(obj, true);

for each(mem: Object in members) {
    if(!(mem is Function))
        continue;

    var func: Function = Function(mem);

    //I want something like this:
    if(func.getName().startsWith("xxxx")) {
        func.call(...);
    }

}

나는 이것을하는 데 많은 것을 찾는 데 어려움을 겪고 있습니다. 도와 주셔서 감사합니다.

도움이 되었습니까?

해결책

당신의 의사 코드는 당신이 원하는 것을하는 것과 가깝습니다. 그러나 개인 메소드를 얻을 수있는 getMemberNames를 사용하는 대신 간단한 루프로 멤버를 고리하고 괄호를 사용하여 멤버의 값을 얻을 수 있습니다. 예를 들어:

public function callxxxxMethods(o:Object):void
{
  for(var name:String in o)
  {
    if(!(o[name] is Function))
      continue;
    if(name.startsWith("xxxx"))
    {
      o[name].call(...);
    }
  }
}

다른 팁

Dan Monego의 대답은 돈에 관한 것이지만 역동적 인 회원에게만 효과가 있습니다. 모든 고정 인스턴스 (또는 정적) 멤버의 경우 flash.utils.describetype을 사용해야합니다.

var description:XML = describeType(obj);

/* By using E4X, we can use a sort of lamdba like statement to find the members
 * that match our criteria. In this case, we make sure the name starts with "xxx".
 */
var methodNames:XMLList = description..method.(@name.substr(0, 3) == "xxx");

for each (var method:XML in methodNames)
{
    var callback:Function = obj[method.@name];
    callback(); // For calling with an unknown set of parameters, use callback.apply
}

동적 및 고정 멤버가 혼합되어 있다면 Dan의 답변과 함께 사용하십시오.

나는 약간의 작업을 수행하고 두 가지 접근 방식을 결합했습니다. 당신을 염두에 두십시오. 그것은 공개적으로 보이는 회원들에게만 작동합니다. 다른 모든 경우 Null은 반환됩니다.

    /**
     * Returns the name of a function. The function must be <b>publicly</b> visible,
     * otherwise nothing will be found and <code>null</code> returned.</br>Namespaces like
     * <code>internal</code>, <code>protected</code>, <code>private</code>, 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 <code>null</code> if no match was found.</br>
     *          In that case it is likely that the function is declared 
     *          in the <code>private</code> 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;                                        
    }

greetz,

독스

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top