ActionScript 3.0 using closures for event handlers

前端 未结 9 1923
挽巷
挽巷 2020-12-28 18:35

I tried doing this:

root.addEventListener(\"click\", 
   function () 
   { 
      navigateToURL(ClickURLRequest,\"_self\"); 
   });

And it

相关标签:
9条回答
  • 2020-12-28 19:34

    Here's a generic way of removing event listeners that i have used on production projects

    
    addEventListener
    (
        Event.ACTIVATE, 
        function(event:Event):void
        {
            (event.target as EventDispatcher).removeEventListener(event.type, arguments.callee)             
        }
    )
    
    0 讨论(0)
  • 2020-12-28 19:34

    You can think of the function() keyword as a constructor, creating a new object (a closure) each time. Therefore, if you create the closure just for as a parameter and don't retain a reference anywhere, there's no way to get a hold of "the same" closure somewhere else.

    The obvious solution is what you don't like, defining the function before using it. Of course, it can still be a full closure and not just a 'static-like' function. simply define it in the context you want, and assign it to a local variable.

    0 讨论(0)
  • 2020-12-28 19:36

    I dont know what you're actually doing but in this particular example perhaps you could have a _clickEnabled global variable.

    Then inside the event handler you just check _clickEnabled, and if its false you just return immediately.

    Then you can enable and disable the overall event without detaching and reattaching it.

    0 讨论(0)
提交回复
热议问题