Actionscript 中内联闭包/函数委托的使用

发布于 2024-07-06 17:12:17 字数 585 浏览 9 评论 0原文

为什么 Actionscript 中很少使用内联闭包? 它们非常强大,而且我认为很有可读性。 我几乎没有看到有人使用它们,所以也许我只是看错了代码。 Google 在其 Google Maps API for Flash 示例中使用它们,但我认为这是我见过它们的唯一地方。

我喜欢它们,因为您可以在定义它们的范围内访问局部变量,并且可以将逻辑保留在一种方法中,并且最终不会得到许多必须为其命名的函数。

使用它们有什么问题吗? 它们的工作方式与 C# 中的工作方式几乎相同吗?

事实上,我刚刚发现 AS3 支持它们,我很生气,因为我以为我读到它们在 AS# 中已被弃用。 所以我又开始使用它们了!

private function showPanel(index:int):void {    

_timer = new Timer(1000, 1);        
_timer.addEventListener(TimerEvent.TIMER, function(event:Event):void 
{
    //  show the next panel
    showPanel(index++);
});

Why are inline closures so rarely used in Actionscript? They are very powerful and I think quite readable. I hardly ever see anyone using them so maybe I'm just looking at the wrong code. Google uses them in their Google Maps API for Flash samples, but I think thats the only place I've seen them.

I favor them because you have access to local variables in the scope that defines them and you keep the logic in one method and dont end up with lots of functions for which you have to come up with a name.

Are there any catches of using them? Do they work pretty much the same way as in C#.

I actually only just discovered that AS3 supports them, and I'm quite annoyed becasue I had thought I read that they were deprecated in AS#. So I'm back to using them!

private function showPanel(index:int):void {    

_timer = new Timer(1000, 1);        
_timer.addEventListener(TimerEvent.TIMER, function(event:Event):void 
{
    //  show the next panel
    showPanel(index++);
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

何其悲哀 2024-07-13 17:12:17

需要注意的最大问题是,在内联闭包中通常没有定义“this”。 有时您可以设置“this”,但并不总是您可以设置的正确“this”,具体取决于您使用它们的方式。

但我想说的是,我所处理的大多数 Flex 代码在整个代码中都大量存在内联闭包——因为回调是完成工作的唯一方法,而且通常您不需要引入整个单独的函数。

有时当函数嵌套太多时,我会在函数中使用函数变量稍微打破它; 这可以帮助我通过为函数提供标签来进行一些组织,但保留内联闭包的一些特征(例如,访问局部变量)。

希望这可以帮助。

The biggest gotcha to watch out for is that often 'this' is not defined in the inline closure. Sometimes you can set a 'this', but it's not always the right 'this' that you would have available to set, depending on how you're using them.

But I'd say most of the Flex code I've worked on has had inline closures rampantly throughout the code--since callbacks are the only way to get work done, and often you don't need the bring out a whole separate function.

Sometimes when the function nested is getting to be too much, I'll break it out slightly with Function variables in the function; this helps me organize a bit by giving labels to the functions but keeping some of the characteristics of inline closures (access to the local variables, for example).

Hope this helps.

也只是曾经 2024-07-13 17:12:17

另外一个问题是垃圾收集在涉及闭包时会被破坏(至少在 Flash 9 中)。 给定闭包的第一个实例(从词法角度来看)永远不会被垃圾收集——以及作用域链中闭包引用的任何其他内容。

One additional problem is that garbage collection is broken when it comes to closures (at least in Flash 9). The first instance of a given closure (from a lexical standpoint) will never be garbage collected - along with anything else referenced by the closure in the scope chain.

意中人 2024-07-13 17:12:17

我发现最初让我不想这样做,但我忘记了细节:

http://livedocs.adobe.com/flex/3/html/16_Event_handling_6.html#119539

(这是米奇提到的 - 就“this”关键字超出范围而言)

所以那就是Adobe的答案,但是我更有可能需要引用局部变量而不是“this”。

其他人如何解读 Adob​​e 的建议?

I found what originally made me NOT want to do this, but I had forgotten the details:

http://livedocs.adobe.com/flex/3/html/16_Event_handling_6.html#119539

(This is what Mitch mentioned - as far as the 'this' keyword being out of scope)

So thats Adobe's answer, however I am much more likely to need to refer to local variables than 'this'.

How do others interpret Adobe's recommendation ?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文