flash - AS3 词典问题

标签 flash actionscript-3

关于 Actionscript 3 中的 Dictionary 类的几个问题:

  • 检查未使用 key 的最佳方法是什么?我在做dictionary[key] == undefined马上。这是最快最干净的方式吗?
  • 我是否必须遍历和delete dictionary[key]或者我可以让字典超出范围吗?
  • 是否有更好的解决方案将消息监听器映射到广播类?我做这样的事情:
    addListener(function : Function, messageType : Type)
    {
        if(dictionary[messageType] == undefined)
            dictionary[messageType] = new Vector<Function>();
    
        dictionary[messageType].push(function);
    }
    
    broadcast(message : Message, messageType : Type)
    {
        if(dictionary[messageType] != undefined)
        {
            for each(var function : Function in dictionary[messageType])
                function(message);
        }
    }
    

  • 我现在才打出来,所以它可能不是 100% 准确。使用带有字典的路由系统的好主意?

    最佳答案

    1 -- 您有两个有效选项:与 undefined 比较或与 null 比较.区别是这样的:

  • undefined : 值根本不存在
  • null : 值存在,但包含空值

  • 因此,您可以选择适合您情况的内容。请参阅示例。
    import flash.utils.Dictionary;
    
    var test:Dictionary = new Dictionary();
    
    trace(test[1] == null); // true, because null is internally converted to undefined
    trace(test[1] === null); // false, because of strictly typed comparison
    trace(test[1] == undefined); // true
    trace(test[1] === undefined); // true
    

    2 -- 当我在字典中有引用时,我总是循环遍历字典来清理它们(不仅仅是像数字或字符串这样的简单类型)。嗯,应该没有必要,但是这样我可以帮助垃圾收集器一点点,这通常是一个好主意。

    3 ——这让我很困惑。为什么需要这样广播?它看起来很像我们几天前在 AS1-2 中的 AsBroadcaster 类,它非本地为我们提供了广播功能。 AS3 有一个本地事件调度系统,您可以升级它以满足您的需要(例如,如果您需要为每个事件类型维护一个监听器列表)。

    这些链接可能有用:
  • http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/events/EventDispatcher.html?filter_flex=4.1&filter_flashplayer=10.2&filter_air=2.6
  • Whats the appropriate form when dispatching events in AS3?
  • 关于flash - AS3 词典问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6514232/

    相关文章:

    apache-flex - 如何在排序后刷新 Flex 数据网格的项目渲染器?

    flash - Adobe Flash Professional CS5编译错误日志-持续集成/每夜构建

    javascript - ExternalInterface.addCallback as3 不起作用

    actionscript-3 - 是否可以从其他包访问包外的类?

    apache-flex - FMS 服务器技术 : 10 connections vs. $4.5K

    flash - 使用Silverlight/Flash预览PDF和PowerPoint文件

    actionscript-3 - AS3 mouseChildren = false 将事件监听器添加到子级

    actionscript-3 - AS3 : declaring an "undefined" parameter for int, uint 或数字

    actionscript-3 - 根据 Flex 中的多个条件更改数据网格单元格的背景颜色

    flash - ActionScript DoABC 标签中的奇怪方法