Re: [flexcoders] var myVar : Function = new Function ? examples?
On Thu, May 20, 2010 at 6:22 AM, Nick Middleweek wrote: > I was going to use the approach for my URLLoader complete event handler > function, I was hoping to keep things a bit neater and just pass the .data > through to my Cairngorm Command. We implemented something similar at work. I suggest that instead of creating a "new" function, simply use a reference to an existing function. For example, in the class that dispatches the command, create a function such as... private function processCmd( /* result data */ ):void { // functionality to handle result here } Then, add a function property to the command event... private var _result:Function; public function get result():Function { return _result; } public function set result( value:Function ):void { _result = value; } You can set the function in the command event (cmd) by the following statement... cmd.result = processCmd; // note that parentheses are not included or it would be a function call instead of reference In the processing of the command event, when you have received data simply call... if ( cmd.result != null ) { cmd.result( /* result data */ ); } Does that make sense? Does it address your problem? Kerry
Re: [flexcoders] var myVar : Function = new Function ? examples?
Neva! :D Just never use anonymous functions in AS3, they are badly implemented and, they aren't truly anonymous. You cannot design a first order functions in ECMAScript languages, nada!, whatever anyone is going to say about that :) Simply give it a normal method, this is the simplest and the best way to go about it. When you pass a function of that kind, especially if you want that to be an event listener these things will happen: 1. Each time you call the function that adds a listener *new* anonymous function is generated. 2. This sort of listener will not be garbagecollected normally. It essentially violates the rule of the stack that local variables should not outlive their scope. The parent function scope is added to that anonymous handler, so, by using it you may potentially run into hard to identify memory leaks. 3. These functions and JIT compiler aren't good friends. Which means they are going to be in general slower (sometimes, however you may compensate it by not invoking it with arguments, so, you'll save on creating arguments array, but that's a poor compensation). 4. Difficult to debug. These functions will appear like either: or MethodInfo-(123) in the call stack trace. Lastly, the way that you have it now, if your server will take a bit to long to reply, both the loader and its handler will be GC'ed, because they are not referenced anywhere outside the function, so, potential of IO error events and unpredictable behavior. Best. Oleg
Re: [flexcoders] var myVar : Function = new Function ? examples?
ok, thanks Oleg... I was going to use the approach for my URLLoader complete event handler function, I was hoping to keep things a bit neater and just pass the .data through to my Cairngorm Command. What are your thoughts on the idea? something like this... private funtion loadXML(path:String) : void { var myReq : URLRequest = new URLRequest(path); var myLoad : URLLoader = new URLLoader(); var completeHandler : Function = function (evt : Event) : Type { // handle data load } myLoad.addEventListener(Event.COMPLETE, completeHandler) myLoad.load(myReq); } Cheers, Nick On 20 May 2010 11:46, Oleg Sivokon wrote: > > > You cannot construct a function like that. What you can do is more like > this: > > var f : Function = function (arguments) : Type { function body } > > -- Sent by Nick Middleweek ( { email: n...@middleweek.co.uk, mobile: +44(0)774 035 5424 } );
Re: [flexcoders] var myVar : Function = new Function ? examples?
You cannot construct a function like that. What you can do is more like this: var f : Function = function (arguments) : Type { function body }
[flexcoders] var myVar : Function = new Function ? examples?
Hello, I can't find any examples on how to create Functon variables? Does anyone have any to hand please? Thanks, Nick -- Sent by Nick Middleweek ( { email: n...@middleweek.co.uk, blog: http://blog.middleweek.co.uk } );