Actually, the "need" is dependent on the implementation.

It was my understanding that AS first looks to the local scope for the existence of a variable then works up to find it. By using "this" you were explicitly telling flash that the var isn't local to the function but rather belongs to the object cutting out a step for the VM.

Therefore, something like the following becomes possible and the use of "this" becomes necessary:

class MyConstructor {
  private var target:MovieClip;
  public function MyConstructor (target:MovieClip) {
    this.target = target;
  }
}

Because you can not do this:

class MyConstructor {
  private var target:MovieClip;
  public function MyConstructor (target:MovieClip) {
    target = target; // ?
  }
}

However, depending on your naming conventions you "might" not have to use "this" if you did something like the following:

class MyConstructor {
  private var _target:MovieClip;
  public function MyConstructor (target:MovieClip) {
    _target = target;
  }
}

Even still, I think the VM might check for the existence of a var named _target local to the constructor function before locating the object's field named _target.

It's been a while since I've done anything in AS1 so I may be way off here but I thought I remembered "this" being necessary because at weird times the VM would think you were trying to instantiate a local var if you didn't use "this". Particularly in "on (something)" event handlers. Maybe I'm thinking of _global, or perhaps both. This, I'm not sure about... if you'll excuse the pun. :)


James O'Reilly  —  Consultant
Adobe Certified Flash Expert
http://www.jamesor.com
Design • Code • Train



Steven Sacks | BLITZ wrote:
There's no need to use "this" when referring to class variables from
within the class itself.

class ArrayTest {
        public var myArray:Array;
        
        public function ArrayTest() {
                myArray = [];
        }
        public function test(str:String):Void {
                myArray.push(str);
                trace(myArray);
        }
}

BLITZ | Steven Sacks - 310-551-0200 x209


-----Original Message-----
From: [EMAIL PROTECTED] [mailto:flashcoders-
[EMAIL PROTECTED] On Behalf Of Jon Bennett
Sent: Tuesday, October 10, 2006 12:44 PM
To: Flashcoders mailing list
Subject: Re: [Flashcoders] Moving to AS2, array always undefined


Or you can do it in the class' constructor:

class ArrayTest {
  public var aryItems:Array;
  public function ArrayTest ()
  {
    this.aryItems = new Array();
  }
  public function test (str:String):Void
  {
    this.aryItems.push (str);
    trace (this.aryItems);
  }
}


_______________________________________________
[email protected]
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com

Reply via email to