0

所以假设我有以下代码:

public function first(text:String):String {
   _text = text;
   dispatchEvent(event);

   //Want this statement to return the value of _text
   //after handler has finished transforming text.
   return _text;
}

//handles the event
public function handler(event:Event):void {
   //does things, then changes the value of _text
   _text = "next text that first needs to return";
}

我如何确保方法(first)在被(handler)转换后返回正确的_text值?

先感谢您!

4

1 回答 1

0

由于 ActionScript 是单线程语言并且事件处理程序不返回值,我假设如果 _text 在包范围内是变量,您的代码将可以工作。下一个代码意义不大,但如果你first从另一个类调用函数,它会很有用

package
{
    import flash.display.Sprite;
    import flash.events.Event;


    public class EventTest extends Sprite
    {
        public function EventTest()
        {
            addEventListener("sliceText", sliceHandler);

            //will be Some
            var newText:String = first("SomeText");
            trace(newText);
        }

        private var _text:String;

        public function first(text:String):String
        {
            _text = text;

            dispatchEvent(new Event("sliceText"));

            return _text;
        }

        protected function sliceHandler(event:Event):void
        {
            //let's slice text to be more valuable
            _text = _text.slice(0,4);
        }

    }
}
于 2012-04-17T05:59:46.590 回答