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

时间:2020-11-30 18:57:35

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.

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

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#.

有没有使用它们?它们的工作方式与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!

我实际上只是发现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++);
});

3 个解决方案

#1


3  

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.

值得注意的最大问题是内联闭包中通常没有定义'this'。有时你可以设置一个'this',但是你可以设置它并不总是正确的'this',这取决于你如何使用它们。

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.

但我要说的是,我所使用的大多数Flex代码都在整个代码中都有内联闭包 - 因为回调是完成工作的唯一方法,而且通常你不需要带来一个完整的单独函数。

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.

希望这可以帮助。

#2


2  

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.

另一个问题是,当涉及到闭包时,垃圾收集会被破坏(至少在Flash 9中)。给定闭包的第一个实例(从词汇的立场)永远不会被垃圾收集 - 以及范围链中闭包引用的任何其他东西。

#3


1  

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'.

这就是Adobe的答案,但是我更有可能需要引用局部变量而不是'this'。

How do others interpret Adobe's recommendation ?

其他人如何解释Adobe的推荐?

#1


3  

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.

值得注意的最大问题是内联闭包中通常没有定义'this'。有时你可以设置一个'this',但是你可以设置它并不总是正确的'this',这取决于你如何使用它们。

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.

但我要说的是,我所使用的大多数Flex代码都在整个代码中都有内联闭包 - 因为回调是完成工作的唯一方法,而且通常你不需要带来一个完整的单独函数。

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.

希望这可以帮助。

#2


2  

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.

另一个问题是,当涉及到闭包时,垃圾收集会被破坏(至少在Flash 9中)。给定闭包的第一个实例(从词汇的立场)永远不会被垃圾收集 - 以及范围链中闭包引用的任何其他东西。

#3


1  

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'.

这就是Adobe的答案,但是我更有可能需要引用局部变量而不是'this'。

How do others interpret Adobe's recommendation ?

其他人如何解释Adobe的推荐?