質問

オブジェクトの各メンバーを反復処理しようとしています。メンバーごとに、それが関数であるかどうかを確認します。それが関数の場合は、その名前を取得し、関数の名前に基づいて何らかのロジックを実行したいと考えます。しかし、それが可能かどうかはわかりません。それは...ですか?任意のヒント?

例:

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を使用する代わりに、単純なfor..inループを使用してメンバーをループし、角かっこを使用してメンバーの値を取得できます。例:

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、

tox

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top