views:

298

answers:

3

I have searched up and down the internet for even the slightest mention of the 'this' reference, and have found NOTHING!!! Nobody seems to talk about 'this' at all. I'm an experienced c++ programmer, so I am more than familiar with the idea of a 'this' pointer/reference. My question is just what exactly is the 'this' reference in an as3 class considered to be? Trying to edit it throws compiler errors. Assigning it to another class instance seems to make a copy of what 'this' "points" to. All my attempts to ID it show it as indistinguishable from a normal variable class instance. My ultimate goal is to point a class instance to a different instance, and I've been trying to determine if I can us 'this' to simplify the process. Thanks!

+1  A: 

The short answer is: No. You won't be able to re-use this to point to a different instance of the same class.

The this keyword in AS3 behaves the same way it would in C#: when used inside of a class, it always references the current instance of the object.

public class Cat
{
    var name:String = "";

    public function getName():String
    {
        return this.name;
    }

    public function setName(name:String):void
    {
        this.name = name;
    }
}

var joe:Cat = new Cat();
joe.setName("Joe"); //joe.getName() will return "Joe"

var john:Cat = new Cat();
john.setName("John"); //john.getName() will return "John"
Justin Niessner
A: 

A quick google gives me these :

http://www.adobe.com/support/flash/action%5Fscripts/event%5Fmethods/event%5Fmethods05.html

http://www.daveoncode.com/2009/01/07/we-should-always-use-actionscripts-this-keyword/

It just refers to the current instance of the object that the method is on.

inferis
+1  A: 

this is a reference to the INSTANCE of the class. It is really for convenience and clarity more than anything - ie it allows for easy access to code hinting and explicitly states what property/method you are attempting to access. It is a reserved word, so you can't use it for other purposes.

It can be very handy with methods that take an argument that is of the same name as a class property such as:

package
{
    public class MyClass
    {
     private var stuff:String;

     public function MyClass()
     {
     }

     public function setStuff(stuff:String):void
     {
      this.stuff = stuff;
     }
    }
}

this.stuff refers to the instance property and stuff refers to the method argument that has been passed in. Without this you would have an ambiguous reference to stuff and the compiler wouldn't know that you wanted to set the class property to the argument.

package
{
    public class MyClass
    {
     private var stuff:String;

     public function MyClass()
     {
     }

     public function setStuff(stuff:String):void
     {
      stuff = stuff; //NO this KEYWORD!
     }
    }
}

would simply set the argument stuff to itself and nothing would happen.

Many feel that the use of this creates cluttered ugly code. Personally I use it quite a bit, but it is a matter of personal taste and the conventions your team/project has agreed to.

You can't reassign this to another class.

Joel Hooks