Advanced Function Work

In this post I’m going to show you a few more advanced features of AS3 functions. I think you’ll find they certainly help, at least when it comes to coding an API for the general public.

…rest Parameter

The rest parameter is cool little parameter type in AS3. It allows for varying sizes of parameter lists to be passed into functions. Let’s look at a quick example:

1
public function foo(bar:String, foobar:Object, ...rest):void

In this example, we declare function called foo, which has two defined parameters called bar and foobar, and then as the last parameter we have this thing called …rest. As you can tell, …rest is the rest parameter. The rest parameter is declared by three¬†ellipses or dots (…) and then followed by the parameter’s name. In this case I’ve named it rest, but you could name whatever you like. For example:

1
public function foo(bar:String, foobar:Object, ...someBigListOfWhoKnowsWhat):void

What does this look like for the person who’s calling the function? Let’s take a look:

1
foo("bar", {foobar: null}, "some", "Big", "List", "Of", "Strings"):void

As you can see, when we call this function we pass in the two defined parameters, and then comes a big long list of parameters that we didn’t define; this is how the rest parameter works, the user can put in as many parameters as they like. It’s important to note, that the user can also pass in parameters of any type, so you’ll have to do some checking if you want the parameters to be of a certain type.

Now we’ll get onto how you can actually access these parameters passed in. When the function actually starts executing it’s statements, the rest parameter has been converted into an array. So we can access the parameters like this:

1
2
3
4
5
6
public function foo(bar:String, foobar:Object, ...otherParams):void {
trace(otherParams.length);
trace(otherParams[0]);
trace(otherParams.shift());
otherParams.unshift("tom");
}

As you can see, we can perform various array methods on it, access the length property, and access the elements in the array. We could also do a for each or for loop on the array, and so on. But what if we need to pass these parameters on to another function, expecting a rest parameter (not an array) itself? That’s what we’ll discuss next.

Function.apply()

When you define a function, you are creating a Function object. The object itself has a number of handy methods. One of those methods we are going to talk about today, and that it the apply() method.

The apply method allows you to call a function, but pass in the parameters needed as an array. Let’s write some setup code:

1
2
3
4
public function foo(bar:String, ...otherParams):void {
// bar is a string we need to do some processing on
var bar2:String = bar.replace(/\$\d/, someArray[0]);
}
1
 
1
2
3
public function bar(foo:String, ...otherParams):void {
// some code to do something with these parameters
}

Now what we need to do is when someone calls foo, we want to do some processing on the bar string as seen in foo(), and then pass on the processed string and all the parameters the …otherParams rest parameter to bar() and return whatever bar() returns. How do we that when, inside the actual function, otherParams is an array (meaning we can’t just go

1
return bar(bar2, otherParams);

). This is where apply() comes in:

1
2
3
4
5
6
7
public function foo(bar:String, ...otherParams):void {
// bar is a string we need to do some processing on
var bar2:String = bar.replace(/\$\d/, someArray[0]);
var applyParams:Array = otherParams;
applyParams.unshift(bar2);
return bar.apply(this, applyParams);
}

So what we’re doing that’s different is, creating an array called applyParams; this is the array of parameters to pass to apply(). Then we are adding the processed string (bar2) to the front of the applyParams array. Finally, we are calling the apply method of the bar object (which is a Function), passing in this (this object) and the applyParams array and returning the returned value. The apply method will use the array as the list of parameters for the function and everything will work smoothly. But why do need to pass in the “this” reference? The first parameter of apply, (so far as I understand it) is what object to point to wherever “this” is referred to in the function. Therefore, be careful with that one and take into account where the function is located.

Anyway, that wraps up this post, hope you learnt a thing or two about functions. I could say more, but I’ll leave it at that. Please feel free to post your thoughts below.

Share on FacebookShare on Google+Share on StumbleUponTweet about this on TwitterEmail this to someone

Facebook comments:

Leave a Reply