Referencing “this” in AS3

Someone recently asked me why should they use the “this” keyword? My response was that you can use a reference to “this” as an easy way to disambiguate instance members from method parameters, local variables and so forth. In certain cases such as dynamic classes, the use of the “this” keyword is required.

After giving a little more thought as to why I originally began utilizing references to “this”, I quickly realized that it began back when AS2 first came out. At the time, I was a Flash engineer who specialized in Object Oriented design in Flash / ActionScript. There were a lot of Flash developers who would use references to _root as a way of dealing with scoping issues specific to Flash. This was definitely a bad idea as application requirements would demand scalability and if ever anything needed to change things would begin to break throughout the application. I began advocating the use of the “this” keyword to my team as a way of enforcing best practices and standards within our organization, but most of all, to eliminate convoluted scope references which would lead to applications failing to scale as needed.

When I moved over to the Flex world during the Flex 1.5 days, I would see the same kinds convoluted references to _root… so again, I quickly began enforcing the use of the “this” keyword to other team members.

So why do I still use the this keyword you might ask? Is there any real benefit? Not really. In reality the “this” keyword is the exact same construct as the “this” pointer in C++, as it is implied by the compiler if not explicitly specified. “this” simply points to the calling object’s pointer variable underneath the covers so to speak.

In the following, both examples are quite literally identical as far as the compiler is concerned:

However, in certain situations using a reference to “this” is required, such as dynamic classes as I had mentioned earlier.

The actual AS3 definition of the “this” keyword as defined by Adobe is:

“A reference to a method’s containing object. When a script executes, the "this" keyword references the object that contains the script. Inside a method body, the "this" keyword references the class instance that contains the called method.”

This is exactly my point. If ever you see a reference to this, you automatically know that the object being referenced is a property or method of the class instance in which you are working. You don’t have to figure anything out.

So it is a matter of personal preference. Again, I mainly use references to this out of habit, but also as a quick way to disambiguate instance properties from method parameters and so forth. Some people prefer to name all instance properties with an under score, and parameters without an underscore, and vise-versa. I personally don’t. Using references to this works for me and it may also work for you, or it might not.

So again, it is a matter of personal preference, and in the end it is simply a matter of whatever works best for you.

{ 2 comments to read ... please submit one more! }

  1. This (yuk yuk) has been bothering me for some time now. I finally get it. I didn’t know you could call your properties that way. I’ve always disliked the underscore approach. 6 of one, half a dozen of the other when it gets down to it, but this just tells it like it is without the odd decorations.

  2. The use of the “this” keyword is a personal coding style preference which has it’s benefits. The reference of “this” instructs the IDEs intellisense to display a list of all instance properties and methods, without the need to use CONTROL+click. More importantly, using references to “this” helps developers who are not familiar with your code to automatically know that any reference to “this” is a reference to an instance member, not a local variable, method parameter and so on. I find it very useful and have been using it for years in ActuionScript.

{ 0 Pingbacks/Trackbacks }