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().


Apr 3 2009

Tutorial: How to install midlets directly from your PC on a mobile phone

Most people think that it is only possible to install applications/games via OTA (over the air) installation directly from a webpage to the phone. Well, that is the most common way and maybe the easiest, but since there are at least the internet costs involved I show another way to install an mobile application. In the way I describe you need to download the desired application first to your local PC.

There are plenty of sources in the net from where you can download various games and applications for free (e.g. see: German CHIP magazin Top 20 free Handygames.)

Disclaimer:
The following tutorial was done and tested only with Sony Ericsson C702 model. It might not work with other models. In addition, use the instruction at your own risk. I will not be held responsible for any claim, loss, damage or inconvenience caused as a result of any information found on this website.

1.) Download a .JAR file from an internet site to you computer.

2.) Plug in your phone (SE C702) to your computer via USB cable.

3.) After software driver installation (should run automatically) you can browse the content of your mobile with the Windows Explorer.

4.) Navigate to “Phone Memory\webpage\saved_pages” on your phone and copy the downloaded .JAR files into that folder.

5.) Unplug your phone from your computer.

6.) Start your mobile internet browser (Note: No internet connection needed - you just need to start it).

7.) Select “Saved pages” in the options menu.

8.) You should see your downloaded game here. Simply click on it and the phone should install it.

This way you by-pass any additional internet traffic and save some money. Since this little manual was only tested on Sony Ericsson C702 model - please feel free to share your experience on other models. Have fun.