ActionScript 3.0 using closures for event handlers

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

I tried doing this:

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

And it

9条回答
  •  天命终不由人
    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.

提交回复
热议问题