Monthly Archives: March 2009

Another use for ‘this’

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!

?View Code ACTIONSCRIPT
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class A 
{
	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
		//myCoolFunction.
	}
 
	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.

This is a good time to use ‘this’

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:

?View Code ACTIONSCRIPT3
1
2
3
4
5
6
7
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’?

What makes you a good Developer? Part 1.

Ya, coding conventions!

(for the OCD developer)

  1. 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:

    Do this:

    public function set label(value:String):void

    Not this:

    public function set label(lab:String):void
  2. 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.
  3. 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.