2011-06-28 64 views
7

ActionScript 3的一对夫妇对Dictionary类问题:AS3字典问题

  1. 什么检查未使用的键的最佳方式?我现在在做dictionary[key] == undefined。这是最快和最干净的方式?

  2. 我必须循环和delete dictionary[key]或者我可以让字典超出范围吗?

  3. 有没有更好的解决方案将消息监听器映射到广播级?我做这样的事情:

    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%准确。对于像这样的字典使用路由系统是个好主意吗?

+0

对于N°1另外还有'in'操作:'如果(在字典中键)'我估计是最好看看。 – RIAstar

回答

2
  1. 你也可以写if (!dictionary[key]) ...

  2. 可以抵消的,而不是循环直通删除所有键的字典对象:dictionary = null;

  3. 我写了一个广播级的,你是绝对欢迎如果你愿意,可以使用它!它对非显示对象之间的全球通信非常有效。但是,如果您想允许显示对象之间进行全局通信,则可以通过舞台添加和派发自定义事件 - 当然,假设它们已添加到舞台上。

Broadcast.as

package com.mattie.events.broadcaster 
{ 
//Class 
public class Broadcast 
    { 
    //Variables 
    public var name:String; 
    public var data:Object; 

    //Constructor 
    public function Broadcast(name:String, data:Object) 
     { 
     this.name = name; 
     this.data = data; 
     } 
    } 
} 

Broadcaster.as

package com.mattie.events.broadcaster 
{ 
//Imports 
import flash.utils.Dictionary; 

//Class 
public final class Broadcaster 
    { 
    //Properties 
    private static var singleton:Broadcaster; 

    private var publicationsProperty:Dictionary; 
    private var subscriptionsProperty:Array; 

    //Constructor 
    public function Broadcaster() 
     { 
     if (singleton) 
      throw new Error("Broadcaster is a singleton that cannot be publically instantiated and is only accessible thru the \"broadcaster\" public property."); 

     publicationsProperty = new Dictionary(true); 
     subscriptionsProperty = new Array(); 
     } 

    //Publish Data 
    public function publish(name:String, data:Object = null):void 
     { 
     publicationsProperty[name] = data; 

     for (var i:uint = 0; i < subscriptionsProperty.length; i++) 
      if (subscriptionsProperty[i].name == name) 
       { 
       var handler:Function = subscriptionsProperty[i].handler; 
       handler(new Broadcast(name, data)); 
       } 
     } 

    //Subscribe Handler 
    public function subscribe(name:String, handler:Function):void 
     { 
     if (publicationsProperty[name]) 
      handler(new Broadcast(name, publicationsProperty[name])); 

     for (var i:uint = 0; i < subscriptionsProperty.length; i++) 
      if (subscriptionsProperty[i].name == name && subscriptionsProperty[i].handler == handler) 
       return; 

     subscriptionsProperty.push({name: name, handler: handler}); 
     } 

    //Unpublish Data 
    public function unpublish(name:String):void 
     { 
     delete publicationsProperty[name]; 
     } 

    //Unsubscribe Handler 
    public function unsubscribe(name:String, handler:Function):void 
     { 
     for (var i:uint = 0; i < subscriptionsProperty.length; i++) 
      if (subscriptionsProperty[i].name == name && subscriptionsProperty[i].handler == handler) 
       { 
       subscriptionsProperty.splice(i, 1); 
       return; 
       } 
     } 

    //Publications Getter 
    public function get publications():Dictionary 
     { 
     return publicationsProperty; 
     } 

    //Subscriptions Getter 
    public function get subscriptions():Array 
     { 
     return subscriptionsProperty; 
     } 

    //Singleton Getter 
    public static function get broadcaster():Broadcaster 
     { 
     if (!singleton) 
      singleton = new Broadcaster(); 

     return singleton; 
     } 
    } 
} 
+5

'if(!dictionary [key])' - 如果'dictionary [key] = false'或'dictionary [key] = 0',这很容易出错。 –

+0

啊,好抓。你是绝对正确的。 – TheDarkIn1978

7

- 你有两个有效的选项:与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 

- 我总是在字典中循环,以清除它们时,我有没有引用(而不是仅仅ptimitive类型,如数字或字符串)。那么,它不应该是必要的,但这样我有点帮助垃圾收集器,这通常是一个好主意。

- 这一个让我感到困惑。你为什么需要这种广播?它看起来很像我们之前在AS1-2中使用AsBroadcaster类的东西,它非本地地为我们提供了广播功能。AS3有一个本地事件调度系统,您可以升级以适应您的需求(例如,如果您需要维护每个事件类型的侦听器列表)。

这些链接可能是有用的:

+0

您也可以使用“in”表达式,例如'如果(键入字典){...}' –