开发者

Access the function object (closure) of a setter

Assume I have the following class:

class Example
{
    function set something(value:String):void
    {
        trace("set something");
    }

    function doSomething():void
    {
        trace("something");
    }
}

I can access the functions as objects like this:

var example:Example = new Example();
var asdf:Function = example.doSomething;
// this also works - example["doSomething"];

asdf(); // this trace: "something"

You do this all the time with events, for example. So, my big question is: Is there any way to get a handle on the setter? Is there some crazy function on Object or somewhere that I don't know about (please say yes :)

I want something lik开发者_Python百科e the following

var example:Example = new Example();

// the following won't work, because example.something is a string
var asdf:Function = example.something; 
asdf("a value"); // this trace: "something"


The statement var asdf:Function = example.something; won't work because compiler treats example.something as a getter and returns string (or throws a write-only error if the getter is not implemented).

But since something is a property, you can do something like:

example["something"] = "a value"; //will trace 'set something'
//or
var property:String = "something";
example[property] = "some value"; //will trace 'set something'


You may try this:

class Example{
  function set something(value:String):void{
    trace("set something");
  }

  function doSomething():void{
    trace("something");
  }
}

class AnotherClass{
  function callOtherClassFunction(funcObj:Obj):void{
    if (funcObj.type == "method") {
      funcObj.func.apply();
    }
    else if (funcObj.type == "setter") {
      funcObj.obj[funcObj.func] = "something";
    }
    else if (funcObj.type == "getter") {
      trace(funcObj.obj[funcObj.func]);
    }
  }
}

class Test{
  function Test():void{
    var e:Example = new Example();
    var a:AnotherClass = new AnotherClass();
    a.callOtherClassFunction({obj:e, type:"setter", func:"something"});
    a.callOtherClassFunction({obj:e, type:"method", func:e.doSomething});
  }
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜