嵌套函数,它们是如何被垃圾Flash ActionScript中3收集的? [英] Nested Functions, how are they garbage collected in flash actionscript 3?

查看:218
本文介绍了嵌套函数,它们是如何被垃圾Flash ActionScript中3收集的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在AS3一个非常复杂的功能。该功能是由嵌套在它的内部,许多其他功能。主要功能有变量和所有它的嵌套函数也有自己的变量。一些的嵌套功能由分别加入的主要功能或在另一个嵌套函数的事件调用

I have a very complex function in as3. The function is composed of many other functions which are nested inside of it. The main function has variables and all it's nested functions also have their own variables. Some of the nested functions are called by events that were added in the main function or in another nested function.

现在我想知道,什么时候我的主要功能是垃圾回收?何时code被淘汰的主要功能?而当将变量和事件和嵌套函数被垃圾收集?

Now I want to know, when will my main function be garbage collected? When will the code be out of the main function? And when will the variables and the events and the nested functions be garbage collected?

P.S:多补充一些细节。想象一下,我的主要函数被调用loadImages(),它包含了一堆嵌套函数:connectToServer(),sortImages()和onImagesLoaded()。 当loadImages()第一次被调用,它会创建一个URL变量和一个图像数组变量。然后,它会调用它的所谓connectToServer内部函数()。此功能将连接到服务器,开始下载图像并添加的onComplete事件侦听器将调用onImagesLoaded()时,所有的图像都被加载。一旦所有图像被加载,因为该事件,onImagesLoaded()被调用。该功能将删除的onComplete事件侦听器,并调用sortImages(),并传递给它加载的图像作为参数。在此之后,sortImages()将图像按字母顺序排序,并把它们的主要功能loadImages的图像阵列()。最后,loadImages()将的AddChild图像阵列到阶段

P.S.: To add some more detail. Imagine my main function is called loadImages() and that it contains a bunch of nested functions: connectToServer(), sortImages() and onImagesLoaded(). When loadImages() is first called, it will create a url variable and an images array variable. Then it will call it's inner function called connectToServer(). This function will connect to the server, start downloading the images and add an onComplete event listener that will call onImagesLoaded() when all images have been loaded. Once all images are loaded, because of the event, onImagesLoaded() is called. This function will remove the onComplete event listener and will call sortImages() and pass it the loaded images as a parameter. After that, sortImages() will sort the images alphabetically and put them in the images array of the main function loadImages(). Finally, loadImages() will addChild the images in the array to the stage.

感谢你。

推荐答案

嵌套函数将会从外部函数捕捉变量和可能preserve这些变量的生活,可能是意外或不必要的。你应该避免使用嵌套函数,除非你特别需要它们提供(捕捉外部变量)的额外功能。

Nested functions will capture variables from the outer function and could preserve the life of those variables, possibly unexpectedly or unnecessarily. You should avoid using nested functions unless you specifically need the extra functionality they provide (capturing the outer variables).

还有使用嵌套函数的性能损失,但它实际上只出现在无用的微基准,不应该影响到现实世界的应用程序。

There's also a performance penalty for using nested functions, but it really only shows up in useless micro-benchmarks that should never affect a real-world app.

如果你需要使用嵌套函数,然后确保你将其取走事件侦听器,当你与他们就大功告成了。这样做,这将使他们可进行垃圾回收。你可以确保你总是把它们注册为弱事件侦听器因此事件不会对他们是否(或者他们捕获的变量)可用于GC进行计数。然而,由于它们是嵌套函数它很可能是唯一保持身边的事件登记。

If you need to use nested functions, then make sure you remove them as event listeners when you're done with them. Doing that will make them available for garbage collection. You can make sure you always register them as weak event listeners so the event will not be counted towards whether or not they (or the variables they captured) are available for GC. However, since they are nested functions it's very likely the only thing keeping them around is the event registration.

这篇关于嵌套函数,它们是如何被垃圾Flash ActionScript中3收集的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆