We should always use Actionscript’s "this" keyword

I was wondering if to use or not the “new” keyword for classes variables and methods, because it’s not mandatory and in as3 examples is rarely used and when it’s used, is only to avoid name collisions (typically when setting a class property using a given argument), such:

package {

    public class MyClass {

        private var myVar:uint;

        public function MyClass(myVar:uint) {

            this.myVar = myVar;

        }

    }

}

However there are 3 valid reasons to (always) use  the “this” keyword:

  1. Is an excellent way to visually differentiate between static and dynamic variable (static variables can’t use the “this” keyword, otherwise you get a compile error)
  2. When invoking methods is immediately evident which is the class owner/target of the method itself
  3. Faster typing and developing thanks to Flex Builder hints (after typing “this.”, I can select all the applicable methods, which is faster and error free than type the entire method name)
  • Amen! It’s amazing how many people are out there lacking the insight of using this keyword. It also provides insight into where variables are defined:

    package {
     public class MyClass {
      private var myVar:String='some text';
      public function MyClass():void {
       this.myVar; // using "this" shows the variable is a member of the class.
       var someOtherVar:String = 'some other text';
       someOtherVar = this.myVar; // not using "this" with someOtherVar shows the variable is declared within the method, either as an argument or defined locally.
      }
     }
    }
    

    This is just something everyone should know and use. It also makes code a lot friendlier for others using the same file.

  • carlos

    i think this is a very good jobe n examples

  • me

    But what does ‘this’ refer to?
    When you say~ this.myVar = myVar; ~what does that mean?

  • “this” refers specifically to the class instance

  • me

    I still don’t get it, that terminology of instances, methods, etc. is a bit confusing.

    Instead of what word was used ‘this’ ?
    What would be ‘the wrong’ way to do this, like without using ‘this’ statement.

    MyClass.myVar = myar ?

  • First of all, I suggest you the excellent book “Essential Actionscript 3”: http://www.amazon.com/Essential-ActionScript-3-0-Colin-Moock/dp/0596526946/ref=sr_1_2?ie=UTF8&s=books&qid=1273249748&sr=1-2

    anyway:

    instance: an object created from a class constructor (ie: instance = new Class())

    method: a function related to a specific class and attached to the instance (ie: instance.method())

  • me

    Thanx for the book reference.
    I found it, I am reading it these days.

  • be aware using the this keyword in inline item renderer’s or in anonymous functions. The this keyword will do the opposite in the above examples. this.myVar will refer to the argument and myVar will refer to the outer scoped class variable.

  • I don’t use “this” keyword in as3. I don’t use Flash Builder so the code hinting part is not a bonus.

    I set up my class vars in the class definition where they are completely obvious by where they are located.

    static vars must be declared static, no confusion there.

    local vars are inside a function and scope is determined by location.

    I do use an underscore prefix when I pass in an argument to a function and then assign it to class scope. If I didn’t do that I would use this.sameName = sameName. But within the function I want to make sure I am using the class scoped var, and it is much easier to type _sameName than wonder if I should type “this.” in front of a var or not.

    My 2

  • Neither of those, I use THIS only inside of a constructor, for assigning a value to an attribute of the same name as the parameter.
    1) never had problem with differentiating static and dynamic attributes
    2) that is evident from the scope and avoid using the same names outside of constructor
    3) -> FlashDevelop – ctrl+space brings up the hints, no need for this
    4) I dont use underscore though, it is very ugly, not easily readable, usually people who transferred from AS2 use it – I use proper variable names