1

此 AS3 函数适用于普通方法和 getter 方法:

   public function MyClassTestAPI(functionName:String, ...rest):* {
    var value:*;            
        try {
            switch(rest.length) {
                case 0:
                    value = myObj[functionName];
                    break;
                case 1:
                    value = myObj[functionName].call(functionName, rest[0]);
                    break;
                case 2:
                    value = myObj[functionName].call(functionName, rest[0],rest[1]);
                    break;
                default:
                    throw("Cannot pass more than 2 parameters (passed " + rest.length + ")");
            }                
        } 
        return value;                
    }

示例用法:

this.MyClassTestAPI("Foo", "arg1"); // tests function Foo(arg1:String):String
this.MyClassTestAPI("MyProperty");  // tests function get MyProperty():String
this.MyClassTestAPI("MyProperty", "new value");// tests function set MyProperty(val:String):void

第三次调用不起作用(抛出异常)。我怎样才能使它也适用于 setter 方法?谢谢!

编辑:
这是一个有效的版本,除了具有附加参数的 getter 和 setter。可以满足我的需求:

   public function MyClassTestAPI(functionName:String, ...rest):* {
    var value:*;            
        try {
            if (typeof(this.mediaPlayer[functionName]) == 'function') {
                switch(rest.length) {
                case 0:
                    value = myObj[functionName].call(functionName);
                    break;
                case 1:
                    value = myObj[functionName].call(functionName, rest[0]);
                    break;
                case 2:
                    value = myObj[functionName].call(functionName, rest[0],rest[1]);
                    break;
                default:
                    throw("Cannot pass more than 2 parameters (passed " + rest.length + ")");
                }                
            }  else {
                switch(rest.length) {
                case 0:
                    value = myObj[functionName];
                    break;
                case 1:
                    myObj[functionName] = rest[0];
                    break;
                default:
                    throw("Cannot pass parameter to getter or more than one parameter to setter (passed " + rest.length + ")");
               }                
            }
        } 
        return value;                
    }
4

2 回答 2

1

Setter functions works as variables, so you can't use it in this way:

    myProperty.call( "new value" );

Your function for variables is pointless, because you just have to do a value assignment:

    myProperty = "new value";

By the way you can include it in your function in two ways:

  1. create a third parameter what tells your function it is a function or variable
  2. create the value assignment in the catch section
于 2011-07-04T11:30:12.280 回答
0

您目前只传递一个值为“新值”的字符串

这应该可以解决问题:

this.MyClassTestAPI("MyProperty", "new","value");

有关此问题的更多信息,请查看 Adob​​e LiveDocs,网址为:http://livedocs.adobe.com/flex/3/html/help.html?content= 03_Language_and_Syntax_19.html

干杯

于 2011-07-04T11:15:06.047 回答