views:

317

answers:

3

Hey there,

I am just wondering when it's necessary to use the keyword this.somevar instead of just somevar.

I usually don't use this under the following circumstances:

  1. i have made a var private for a class
  2. i refer to it in order to use it as a local var for that class

correct? or is this not just a style thing?

+2  A: 

If you use this inside an object this will refer to the object itself. Here is an example where you would use it:

package
{
    public class foo
    {
        private var variable:uint;

        public function foo(variable:uint)
        {
            this.variable = variable;
        }
    }
}
slayerIQ
i was saying that i think this happens anyway.see the comment below; i found it useful. thanks!
jml
+5  A: 

You generally only have to use this.varname if you are inside a function that has a parameter with the same name and you want to refer to the member variable. Whether or not the member var is private does not have any effect on having to use 'this'.

Simon Groenewolt
thanks. this clears up what i was hoping to find out.
jml
+2  A: 

As has already been mentioned, you don't need to use this in a very simple case, where you want to refer to a class member variable from a class member method, e.g.:

public function myClassMethod() : void
{
  // "this" is redundant here, as it is implied if omitted
  this.myPrivateOrPublicVariable = 123;
}

Do note though that there are cases where this (which is evaluated at the time it is encountered in a program, rather than at instantiation as one might guess) does not refer to the class in which it is written. More specifically, this happens when you use it inside a function that will get called using a method such as Function.apply(). This method allows the caller to specify the object that will be referred to by this, and it is commonly used in callbacks, e.g. in tweening engines.

// Call myFunction(listOfArguments[0], listOfArguments[1] ...), 
// with this referring to objectReferredToByThis.
myFunction.apply(objectReferredToByThis, listOfArguments);

Here's an example with the popular tweening engine Tweener, where the default behavior is to have this refer to the tweened object (i.e. the MovieClip _mc), rather than to an instance of the class in which this is written (i.e. MyClass).

package
{
  /* ... imports ... */

  public class MyClass extends Sprite
  {
     private var _mc : MovieClip;

     public function tweenSomething() : void
     {
       var tween : Object = {
         x: 0,
         time: 1, 
         onComplete: function() : void {
           // Here, "this" will not refer to the instance
           // of MyClass, but rather to the _mc MovieClip,
           // which is the object being tweened below.
           this.visible = false;
         }
       };

       Tweener.addTween(_mc, tween);
     }
  }
}

In the above code, the use of this is necessary, because we want to explicitly instruct the runtime to modify the visible state of our MovieClip (_mc). Had we not used this, the visible reference would have referred to MyClass.visible, because of function closure.

richardolsson
thanks for getting into the details of this.i appreciate the clarifications.
jml
hmm, i don't get it why in the tweener example this. refers to the movieclip.
Richards
@Richards, it does because the caller (inside Tweener) specified the tweening target to be reference by the "this" variable. See the documentation about Function.apply(). Cheers
richardolsson