A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: [OOP Basics] Grabbing the parent of a function

  1. #1
    Participant jide's Avatar
    Join Date
    May 2003
    Posts
    264

    [OOP Basics] Grabbing the parent of a function

    I'm having trouble adjusting to the way functions pass in AS3.
    How come when I say "this" it doesn't know I'm refering to AudioTrack?


    PHP Code:
        var AudioTrack:Object = new Object();
        
    AudioTrack.myVar "Hello";
        
        
    AudioTrack.onComplete = function(){
            
    trace (this.myVar)
        }
        
    AudioTrack.onComplete(); 
    glhf

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    According to the livedocs, it should.

    According to my quick test, it does.
    Code:
    package 
    {
    	import flash.display.Sprite;
    	import flash.events.Event;
    
    	public class Main extends Sprite 
    	{
    		
    		public function Main():void 
    		{
    			if (stage) init();
    			else addEventListener(Event.ADDED_TO_STAGE, init);
    		}
    		
    		private function init(e:Event = null):void 
    		{
    			removeEventListener(Event.ADDED_TO_STAGE, init);
    			// entry point
    			var something:Object = new Object();
    			something.afunction = function():* {
    				trace(this.myVar);
    			}
    			something.myVar = 4;
    			something.afunction();
    		}
    		
    	}
    	
    }
    traces "4".

    Forgive the boilerplate code, it's generated for me.

  3. #3
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    Works fine:

    PHP Code:
    var o:Object = {s:"foo"f:function(){return this.s}};
    trace(o.f());   //  foo 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center