Actionscript 3 - First Steps IX

      FLASH 2005-10-19 22:55
The new integrated delegation probably needs some more attention than in Step VII. The next example shows, that this has nothing to do with the event system, which is part of the flash api, but is a feature of the language itself. We create a simple second class Test2, containing a method, that runs a function given as parameter.
package {
	
	import flash.util.trace;
	import flash.display.MovieClip;
	
	public class Test 
		extends MovieClip 
	{
		
		public function Test() {
			var t2:Test2 = new Test2();

                         // use a member method as parameter
			t2.run( onCompleteT2);
			
                         //use a anonymous function as parameter
			var ref:Test = this;
			t2.run( function(){
				trace("Test::anonymous function " + this + ":" + ref);
			});
		}
		
		public function onCompleteT2(){
			trace("Test::onCompleteT2 " + this);
		}
		
		
	}
	
	private class Test2 
	{
		
		public function run( onComplete:Function){
			trace("Test2::run");
			onComplete();
		}
		
	}
}


As you can see from the second trace, the scope of onCompleteT2 is implicitely set to the instance of Test. No longer we need to use Delegate.create or local variables to provide a reference to the scope. Still this method works though, as you can see from the last trace.
Also note, that anonymous functions are no longer automatically scoped to _root but to global, as you can see from the last trace too.
Cheers,
Ralf.

/////////////////
因为是个人读书的笔记,所以没有进行翻译,只是对其中关键地方进行注释。如有错误欢迎指出。
原文转载自: www.helpqlodhelp.com/blog/
标签集:TAGS:
回复Comments() 点击Count()

回复Comments

{commenttime}{commentauthor}

{CommentUrl}
{commentcontent}