关于 Actionscript 3 中的 Dictionary 类的几个问题:
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 有一个本地事件调度系统,您可以升级它以满足您的需要(例如,如果您需要为每个事件类型维护一个监听器列表)。
这些链接可能有用:
关于flash - AS3 词典问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6514232/