May 15 2009

AS3.0 TutBits #04: Untyped variables

When you create a variable in ActionScript you normally add its type, e.g.

var age:uint;

Whenever you assign to that variable a value of another type the compiler will throw type mismatch errors.

// This will result in a type mismatch error
age = -10;

Actionscript provides untyped variables. You can set them explicitly with the datatype “*“, e.g.:

var anything:*;
anything = 10;
anything = "Test";
anything = new Array();

Above code will not throw any type mismatch errors, because the variable is declared as an untyped variable. So you can assign values of any kind to it.

From my personal point of view however, you should avoid using untyped variables whenever you can.


May 8 2009

AS3.0 TutBits #03: Nested functions

Nested functions are functions which are defined inside a method or in other function. They are only accessible in the method/function in which they are defined. Once there is a definition of a function inside a method it does not matter if you call that nested function before its definition or after it. It works in both cases.

Here is a short example of a nested function.

private function doCalculation():void
{
	for (var i:uint; i < 10; i++)
	{
		// Do some serious calculation
		nestedLog("Calculation " + i + " done");
	}
 
	// Nested function
	function nestedLog(logTxt:String):void
	{
		trace(logTxt);
	}
}

The function defined in line 10 is only accessible in the scope of the method doCalculation(). Therefore, it is illegal to add any control access modifiers (public, private, protected) to the function.

Nested functions may be used to store some method-specific subroutines.


Apr 21 2009

Unknown number of parameters in Javascript

In the previous TutBits post, I showed the technique of functions which take an arbitrary amount of parameters in actionscript. Lately, I needed this feature in javascript. In javascript to each function belongs an array containing ALL parameters. The naming of that array follows the following rule:

functionname.arguments

Since this array is created by default, actually you would not need to declare any parameters anymore. But of course it is more convenient to name parameters in the function signature with a variable name.

However, there might be situations where you do not know the exact amount of parameters which are passed to your function. Have a look at the following piece of code which is the equivalent of the actionscript function of the previous post:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
function showFavouriteMovies(name)
{
    var retString = name + "'s favourite movies are: ";
 
    // Loop through all arguments starting from index 1.
    // Note: index 0 contains the value for given explicit parameter 'name'
    for (var i = 1; i < showFavouriteMovies.arguments.length; i++)
    {
        retString += showFavouriteMovies.arguments[i];
 
        // Add comma to list
        if ( i < (showFavouriteMovies.arguments.length-1) )
        {
            retString += ", ";
        }
    }
 
    alert(retString + ".");
}

On line #07 you see how to access the functions’ arguments array. We simply loop through it. Remember that we start in our example at index 1. Index 0 contains the first parameter name which equals showFavouriteMovies.arguments[0].

As you can see, functions in javascript with an unknown number of parameters are easily possible, however a big disadvantage in javascript is in my eyes that you cannot judge by a function’s signature if it processes arbitrary parameters or not. In other languages like Actionscript for example an arbitrary amount of parameters are symbolized by a “…args:Array”-parameter in the function signature.


Apr 16 2009

AS3.0 TutBits #02: Unknown number of parameters

The second TutBit is dealing with creating a method/function which takes an arbitrary number of arguments. This makes the creation of very flexible function calls possible. Have a look at the following example:

private function showFavouriteMovies( name:String, ...movies:Array ):void
{
	var retString:String = name + "'s favourite movies are: ";
 
	for (var i:uint = 0; i < movies.length; i++)
	{
		retString += movies[i];
		if ( i < (movies.length-1) )
		{
			retString += ", ";
		}
	}
 
	trace(retString + ".");
}

The created method called showFavouriteMovies() takes two arguments. The first parameter called “name” is a normal parameter of type String. The second parameter “…movies” defines an array to hold any arguments passed to the method. The name after the “…”-construct defines the name of the array (here it is “movies”).

So, calling the function for example with the following paramters:

showFavouriteMovies("Joshua", "Wargames", "13th Floor", "The beach");

will result in:


Joshua's favourite movies are: Wargames, 13th Floor, The beach.

You see that the first named paramter called “name” can be used normally. The list of movies are stored in an indexed array where the first element (i.e. index 0) is the left-most value of the method call (in our example it is “Wargames”) proceeding to the right.

Always keep in mind, that the “…”-construct must be the last parameter in your method’s signature.


Apr 12 2009

AS3.0 TutBits #01: Getter and Setter

AS3.0 TutBits is a series of small Actionscript mini tutorials. Their purpose is to point out certain differences of the actionscript 3.0 language compared to other programming languages. Each tutorial just concentrates on a tiny technical issue. The AS3.0 TutBits are not tutorials that teaches you how to programm in actionscript.

Ok, enough introductional text - let’s start:

First topic is Getter and Setter. Well, I do no want to start the discussion again if they are useful or not - here it is how they are supported in Actionscript3.0:

package
{
	public class GetterSetter
	{
		private var hiddenName:String = "OLD: Joshua";
 
		public function get name():String
		{
			return this.hiddenName;
		}		
 
		public function set name(newName:String):void
		{
			this.hiddenName = "NEW: " + newName;
		}
	}
 
}

On line #05, we have the class property called hidden name we want to make accessible via getter and setters. Therefore, we create two methods one line #07 and line #12 which are identified by the keyword get and set followed by the same methodname name(). With this construct you create a faked public class property called name of the class GetterSetter. You access it like a normal property using the following syntax:

var gs:GetterSetter = new GetterSetter();
trace(gs.name);
gs.name = "Joshua";
trace(gs.name);

The output of that would be:

OLD: Joshua
NEW: Joshua

That means that when you access the faked class property you actually call the respective get- or set- method. So you have the easiness of simulating direct access of a property value but still have the chance to keep control of it.

Nevertheless, this technique has some drawbacks. First, it is not possible that the property has the same name as the get/set methodname. So, if we would have named our property “name” instead of “hiddenName” it would resulted in a compile-time error. In addition, whenever you call the set method - the get method is called automatically as the result of the set method call. This hidden “feature” could also causes some confusion if you do not know that.

In the end it is up to your personal taste if you want to use this feature of actionscript or not. Of course you could implement getters and setters in the standard way writing methods like getHiddenName() and setHiddenName().