开发者

How to call a setter method with arguments dynamically in flash AS3

This AS3 function works for normal methods and getter methods:

   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;                
    }

Sample usage:

this.MyClassTestAPI(开发者_Go百科"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

The third call does not work (throws exception). How can I make it work for setter methods as well? Thanks!

edit:

This is a version that works, except with getter and setter that have additional parameters. It is ok for my needs:

   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;                
    }


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


You are currently passing only one string with value "new value"

This should do the trick:

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

For more information on this matter check the Adobe LiveDocs at: http://livedocs.adobe.com/flex/3/html/help.html?content=03_Language_and_Syntax_19.html

Cheers

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜