PDA

View Full Version : Why do I delegate when I can apply?



Whaka
February 16th, 2007, 04:35 PM
What is the advantage of using a Delegate class in as2? I realize that it's old news for as3 guys. But Ive got a medium sized as2 project and Im going through my code and wondering why Im using Delegate.create sometimes. What's wrong with "apply"?

I admit that I must have jumped on the Delegate bandwagon without thinking anything more than "dhuu...thats what the pros must be using".

Am I right in saying that there is nothing at all wrong with simply saying "function_name.apply( this, args )"? Or am I missing something? Is it a matter of memory management? I've done a lot of research on delegation and read all kinds of blogs and scripts from little tweeks to the official.. to >200 lines. Yikes! At the end of the day I wrote my own little delegate class that worked for me (just a rewrite of Proxy without loops). But now that I go though my code I see that I might as well have just used apply because that's all Im doing with it!

Id like to be a better programmer. What am I missing and why shouldn't I do a find and replace here. Why is it better to wrap up "apply"? Will I be better readied to learn other languages? If thats it Im all for it. Could you tell me why?

I just don't understand why I was so convinced that I needed the delegate class when I had "apply" out of the box.

Thanks and I hope I didnt take up too much space here.

senocular
February 16th, 2007, 04:55 PM
Thats all Delegate is, a wrapper for apply. Use whatever you want.

TheCanadian
February 16th, 2007, 04:58 PM
The Delegate class is just a wrapper for the Function.apply function.

Delegate does not call the function when you call the create method. Rather, it returns a function which will call the function using the apply method. This can be seen if you take a look at the code used:

static function create(obj:Object, func:Function):Function {
var f = function () {
var target = arguments.callee.target;
var func = arguments.callee.func;
return func.apply(target, arguments);
};
f.target = obj;
f.func = func;
return f;
}

This is useful for changing the scope a function is called in for functions that are broadcast by components or the Flash Player itself. In other words, for all listening functions which may have an unwanted scope.

For example, functions listening to v2 component events will be called in the scope of the broadcasting component, not the scope in which the listener is defined. Delegate will allow you to have the function called in the proper scope of the function. This is actually why the Delegate class was created in the first place, but it has more uses.

Delegate is not in AS3, like you said, because listener functions are called in the scope they are created.

Using delegate allows you to chose the scope for the function to be called in without actually calling it.

Dazzer
February 16th, 2007, 11:39 PM
Delegate is not in AS3, like you said, because listener functions are called in the scope they are created.

Thank goodness. One less thing to worry about.

I've hardly heard of the Delegate class....O.O

MichaelxxOA
February 16th, 2007, 11:42 PM
Just had to express that I too was excited with this :)

Michael

senocular
February 17th, 2007, 10:33 AM
Thank goodness. One less thing to worry about.

I've hardly heard of the Delegate class....O.O

Its one less thing to worry about and one more thing to worry about. If you're used to doing something like this:

function moveRight(){
this._x += 5;
}

clip1.onPress = moveRight;
clip2.onPress = moveRight;
You may have to think twice about where you define moveRight since methods are bound to their instances (essentially a forced delegate). That means this in both onPresses is actually the current timeline or the instance where moveRight was defined, not the object from which the function is being called. This also prevents Function.apply and Function.call from working on those methods.

Generally, though, once you get into the mindset of how these things work, its not an issue. And getting by without Delegate (we all had to prior to Flash 7.2) was no big deal either, if you knew what was going on.