Use “this”…

Because “this” is just great!

Unfortunately many people omit them in their code :cry: . Not sure why though, but my guess is that they are ignorant or just lazy (don’t be offended if you don’t use the this keyword for another reason, but please let me know why).
Even Adobe (and Macromedia before them) neglects the this keyword. None of their examples uses this and it’s nowhere to be found in their own code either!
Shame on them!

At the company where I work we didn’t use the keyword up until a year or two/three ago. Some of us found the keyword to be very handy and convinced the rest (not sure which group I belonged to at that time).

First of all, using the this instance variables supplies you the option to declare a local variable with the same name:

1
2
3
4
this.myVar = "my instance var"; 
var myVar:String = "my local var"; 
trace(this.myVar); //my instance var 
trace(myVar); //my local var

(Note that the above code only works in a class and not in the timeline!)

This can come in really handy when you need to store some temporarily data, during a for loop for example.

The second and most important reason to use this, is that it keeps your code clean and neat. It will help you to visually distinguish the local variables from the instance variables. This is vital if you work in a team on the same project or if other people need to work on your project later on (bug fixing, implementing new feature, etc.)
In all these cases it’s important to scan through someone’s code quickly and see at a glance where a certain variable comes from or is declared. If you omit the this keyword you would first have to scan the entire method to see if, maybe, the variable is declared local in this method. While if you do use the this keyword you know for sure it is declared in the Class definition and you know you can use it throughout the entire class (and in case of a public var, throughout the entire code).

So using the this keyword gives you more control over your code and it lets you (and your colleagues) programme in old or other peoples code much faster!

If you don’t use the this keyword at the moment, I hope this post convinced you to try out the this keyword for a while (even if it’s only for one single project) and I’m sure you never want to programme with out it, ever again.

If you already did use the this keyword, maybe this post wil help you to convince some ignorant fools in your vicinity :D .

4 Responses to “Use “this”…”


  • this.blog = “Great”;
    private function traceThis():void{
    var blog = “just”;
    trace(“this blog is “+blog+this.Blog+”!!!”);
    }
    traceThis();

  • Nice one! I’ve been convincing my team members to ‘this’ their classes. Your post helps me with that!

  • Heya-

    One reason to avoid using the “this” keyword is that it has to figure out what “this” is first, before referencing whatever your trying to reference using “this” making it take longer.

    Take the below for example

    this._myVar = this.myVar2;
    myVar = myVar2;

    the second line is faster cause it knows the variables you are trying to edit are within the scope of wherever you are. By using the previous statment, you are telling it to first find “this” then find the variable, or function or what not under it.

    Make Sense?

    _-Menser-_

  • Menser, did you test this theory? It makes some sense but I think the speed difference is too so small to be noticed at all…

Leave a Reply