So just as I was looking over the lase post about ‘this’. I realized that I skipped one of the best uses for the infamous property of ‘this’. Enough talk, more actionscript (sorry terrible pun).
So imagine this. Lets say that we have class A which creates an instance of class B. Now lets say when want to store an instance of class A in class B so that you can call class A methods. For smaller apps this is can be an acceptable code structure. So how do you pass an instance of a class in that same class? You guessed it. this!
private var b:B;
public function A()
b = new B(); //Creates instance of B class
b.a = this; //this passes a reference of class A
//to class B. Now class B can call class A's
public function myCoolFunction():Void
//Magic happens here. :)
If you have any other handy uses for this (I’m sure there are), please let me know in the comments.
I know for a lot of actionscript coders out there, especially beginners, ‘this’ in can be rather confusing. This issue can spark some pretty heated debates about this proper use. So before diving in here is a short explanation of ‘this’. In actionscript the object the code is currently running in is referred to as ‘this’. So lets say a class has a property myVar, putting this.myVar tells actionscript to look in the current object for the property myVar. Why do we need this? This doesn’t make any sense! Well look at the code below:
public var age:Number;
public function myClass(age:Number)
if(age > 0)
this.age = age;
So lets say this is a class (myClass) with a public variable age. In actionscript you can declare local variables and parameters that only exist for the duration of the function. These local variables have the smallest scope, but have the highest preference, meaning if another variable is named the same the local variable, the local variable will be used instead of the class variable. In the above example we have a constructor the class myClass. Can you see were I am going with this? In the above constructor we have two age variables! The parameter is defined in the function. So to set the class variable we need to use ‘this’ to specify between the local variable and the class variable.
This example is the only the real time that you need to use ‘this’, of course this is only my opinion. What do you think? Coding vets how do you feel about ‘this’?
Ya, coding conventions!
(for the OCD developer)
- Follow coding conventions in your projects. That way you write clear and concise code. The problem is there are many conventions. In reality “conventions” is really a code word for someones arbitrary opinion on how to write neat code. So…which one do you follow? It doesn’t really matter; just follow one. Adobe recently lays one out and I can agree with most of it. It is not complete and just because it is by Adobe does not mean it is the end all be all.
While reading the document, I couldn’t help but feel like I was being scolded by my parents. Take a look:
public function set label(value:String):void
public function set label(lab:String):void
- Stick to those conventions! I have to admit it is hard but practice it all the time. Remember this is not the fun part of Flash & Flex development and it never will be but make it a habit.
- Lastly, you will eventually run into someone who doesn’t agree with your conventions. In that situation just think about my first point.
Now time for me to go back to my old code and see what mistakes I’ve made.