PDA

View Full Version : _owner vs Delegate


303 maddec
07-28-2005, 02:29 PM
Hi there,

I'm used to the following practice:

var myVar:String="george";

function myFunctionA (){

var _owner=this;

function myFunctionB (){

trace (_owner.myVar);
}
}

Since I try hard every day to improve my coding to the best practice possible I'm considering the Delegate way of doing this:

var myVar:String="george";

function myFunctionA (){

function myFunctionB (){

trace(Delegate.create(this,myVar));
}
}

Is there a real advantage against the former in term of use and of good practice?

Thank you for your answer.

Best regards.
maddec

hangalot
07-28-2005, 02:39 PM
Delegate makes for neater code IMO, there are things to watch out for however when using delegate with the eventdispatcher mixin, baaad things that cause memory leaks.

303 maddec
07-28-2005, 02:55 PM
I'm currently optimising some of my Classes: about Delegate do you think it's a good choice to reshape internal setInterval:

var myIntervalId=setInterval(this,"myFunctionAfterInterval",50);

by

var myInt = setInterval(Delegate.create(this, myFunctionAfterInterval), 50);

Is there performance issues? I read about stability differences... with the former being considered unreliable... (personnally I never observed such a problem).

Best regards.
maddec

hangalot
07-28-2005, 02:59 PM
no its only unstable when people don't clean up after themselves properly, personally i use delegate in that scenario mostly, but not always. what delegate allows you to do is tab into on named event and call multiple functions from it which is handy.

something in your code that has me concerned is the fact that your intervalId is a local scope variable meaning it will go out of scope at the end of you function, not allowing you to removce your interval. cleaning up after yourself and all that...