Dictionary 和 Object 的区别

1. Dictionary 与 Object的区别在帮助文档中说的很清楚了:

 

Dictionary 类用于创建属性的动态集合,该集合使用全等运算符 (===) 进行键比较。将对象用作键时,会使用对象的标识来查找对象,而不是使用在对象上调用 toString() 所返回的值。Dictionary 集合中的原始(内置)对象(例如 Number)的行为方式与它们作为常规对象的属性时的行为方式相同。

 

并给出了例子:

 

 

var dict:Dictionary = new Dictionary();
var obj:Object = new Object();
var key:Object = new Object();
key.toString = function() { return "key" }
				
dict[key] = "Letters";
obj["key"] = "Letters";
				
dict[key] == "Letters"; // true
obj["key"] == "Letters"; // true 
obj[key] == "Letters"; // true because key == "key" is true because key.toString == "key"
dict["key"] == "Letters"; // false because "key" === key is false
delete dict[key]; //removes the key

 

 

2. 在这里,你还要理解 == 和 === 有何区别

 

在帮助文档中也做了很好的说明:

 

测试两个表达式是否相等,但不执行自动数据转换。如果两个表达式(包括它们的数据类型)相等,则结果为 true


 

全等运算符 (===) 与等于运算符 (==) 在以下三个方面相同:

  • 数字和布尔值按值进行比较,如果它们具有相同的值,则视为相等。
  • 如果字符串表达式具有相同的字符数,而且这些字符都相同,则这些字符串表达式相等。
  • 表示对象、数组和函数的变量按引用进行比较。如果两个这样的变量引用同一个对象、数组或函数,则它们相等。而两个单独的数组即使具有相同数量的元素,也永远不会被视为相等。

全等运算符 (===) 与等于运算符 (==) 仅在下列两个方面不同:

  • 全等运算符仅针对数字类型(Number、int 和 uint)执行自动数据转换,而等于运算符 () 针对所有的原始数据类型执行自动数据转换。
  • 当比较 nullundefined 时,全等运算符将返回 false
并给出了例子:

//下例说明当值和数据类型都匹配时,全等运算符 (===) 与等于运算符 (==) 相同: 
var string1:String = "5"; 
var string2:String = "5"; 
trace(string1 == string2);  // true 
trace(string1 === string2); // true
//下例说明全等运算符不将 String 类型的数据转换为 Number 类型的数据,但是等于运算符 (==) 却进行这样的转换: 
// The equality (==) operator converts 5 to "5", but the strict equality operator does not
var string1:String = "5"; 
var num:Number = 5; 
trace(string1 == num);  // true 
trace(string1 === num); // false 
//下例说明全等运算符不将布尔值转换为数字,但是等于运算符却进行这样的转换: 
var num:Number = 1;
var bool:Boolean = true;
trace(num == bool);  // true 
trace(num === bool); // false下例说明全等运算符确实转换 int 和 uint 数据类型: 
var num1:Number = 1;
var num2:int = 1;
var num3:uint = 1;
trace(num1 === num2); // true
trace(num1 === num3); // true下例说明全等运算符将 null 和 undefined 视为不相等,但是等于运算符却将它们视为相等: 
trace(null == undefined);  // true 
trace(null === undefined); // false 
 
3. 还要注意的是 hasOwnProperty() 方法
指示对象是否已经定义了指定的属性。如果目标对象具有与 name 参数指定的字符串匹配的属性,则此方法返回 true;否则返回 false
(在这里,你出入进去的是string 类型的值,即使你传入的是一个对象,它也会调用对象的toString()方法转化为string 然后进行比较.)
所以,如果你的dictionary的key值是对象,你怎么查找是否拥有此key值呢?
public function containsKey(key:*):Boolean
{
	for(var k:* in dict)
	{
		if(k === key)
		{
			return true;
		}
	}
	return dict.hasOwnProperty(key);
}
 
4.As3 中没有Map 类,不过可以自己模拟一个。
package
{
	import flash.utils.Dictionary;

	public class DictCache implements ICache
	{
		/**
		 *
		 * Defines the underlying object which contains the key / value
		 * mappings of an <code>ICache</code> implementation.
		 *
		 * @see http://livedocs.adobe.com/flex/3/langref/flash/utils/Dictionary.html
		 *
		 */
		protected var dict:Dictionary;

		/**
		 *
		 * Creates a new DictCache instance. By default, weak key
		 * references are used in order to ensure that objects are
		 * eligible for Garbage Collection immediatly after they
		 * are no longer being referenced, if the only reference to
		 * an object is in the specified Dictionary object, the key is
		 * eligible for garbage collection and is removed from the
		 * table when the object is collected 
		 * 
		 *
		 * @example
		 *
		 * <listing version="3.0">
		 *
		 * import com.lombardrisk.utils.DictCache;
		 * import com.lombardrisk.utils.ICache;
		 *
		 * var cache:ICache = new DictCache( false );
		 *
		 * </listing>
		 *
		 * @param specifies if weak key references should be used
		 *
		 */
		public function DictCache(weakReferences:Boolean=true)
		{
			dict = new Dictionary(weakReferences);
		}

		/**
		 *
		 * Adds a key and value to the DictCache instance
		 *
		 * @example
		 * <listing version="3.0">
		 *
		 * import com.lombardrisk.utils.DictCache;
		 * import com.lombardrisk.utils.ICache;
		 *
		 * var cache:ICache = new DictCache();
		 * cache.put( "user", userVO );
		 *
		 * </listing>
		 *
		 * @param the key to add to the cache
		 * @param the value of the specified key
		 *
		 */
		public function put(key:*, value:*):void
		{
			dict[key] = value;
		}

		/**
		 *
		 * Places all name / value pairs into the current
		 * <code>IMap</code> instance.
		 *
		 * @example
		 * <listing version="3.0">
		 *
		 * import com.lombardrisk.utils.DictCache;
		 * import com.lombardrisk.utils.ICache;
		 *
		 * var table:Object = {a: "foo", b: "bar"};
		 * var cache:ICache = new DictCache();
		 * cache.putAll( table );
		 *
		 * trace( cache.getValues() );
		 * // foo, bar
		 *
		 * </listing>
		 *
		 * @param an <code>Object</code> of name / value pairs
		 *
		 */
		public function putAll(table:Dictionary):void
		{
			for (var prop:String in table)
			{
				put(prop, table[prop]);
			}
		}

		/**
		 *
		 * Removes a key and value from the DictCache instance
		 *
		 * @example
		 * <listing version="3.0">
		 *
		 * import com.lombardrisk.utils.DictCache;
		 * import com.lombardrisk.utils.ICache;
		 *
		 * var cache:ICache = new DictCache();
		 * cache.put( "admin", adminVO );
		 * cache.remove( "admin" );
		 *
		 * </listing>
		 *
		 * @param the key to remove from the cache
		 *
		 */
		public function remove(key:*):void
		{
			delete dict[key];
		}

		/**
		 *
		 * Determines if a key exists in the DictCache instance
		 *
		 * @example
		 * <listing version="3.0">
		 *
		 * import com.lombardrisk.utils.DictCache;
		 * import com.lombardrisk.utils.ICache;
		 *
		 * var cache:ICache = new DictCache();
		 * cache.put( "admin", adminVO );
		 *
		 * trace( cache.containsKey( "admin" ) ); //true
		 *
		 * </listing>
		 *
		 * @param  the key in which to determine existance in the cache
		 * @return true if the key exisits, false if not
		 *
		 */
		public function containsKey(key:*):Boolean
		{
			for(var k:* in dict)
			{
				if(k === key)
				{
					return true;
				}
			}
			return dict.hasOwnProperty(key);
		}

		/**
		 *
		 * Determines if a value exists in the DictCache instance
		 *
		 * <p>
		 * If multiple keys exists in the map with the same value,
		 * the first key located which is mapped to the specified
		 * key will be returned.
		 * </p>
		 *
		 * @example
		 *
		 * <listing version="3.0">
		 *
		 * import com.lombardrisk.utils.DictCache;
		 * import com.lombardrisk.utils.ICache;
		 *
		 * var cache:ICache = new DictCache();
		 * cache.put( "admin", adminVO );
		 *
		 * trace( cache.containsValue( adminVO ) ); //true
		 *
		 * </listing>
		 *
		 * @param  the value in which to determine existance in the cache
		 * @return true if the value exisits, false if not
		 *
		 */
		public function containsValue(value:*):Boolean
		{
			var result:Boolean=false;

			for (var key:* in dict)
			{
				if (dict[key] == value)
				{
					result=true;
					break;
				}
			}
			return result;
		}

		/**
		 *
		 * Returns the value of the specified key from the DictCache
		 * instance.
		 *
		 * <p>
		 * If multiple keys exists in the map with the same value,
		 * the first key located which is mapped to the specified
		 * value will be returned.
		 * </p>
		 *
		 * @example
		 * <listing version="3.0">
		 *
		 * import com.lombardrisk.utils.DictCache;
		 * import com.lombardrisk.utils.ICache;
		 *
		 * var cache:ICache = new DictCache();
		 * cache.put( "admin", adminVO );
		 *
		 * trace( cache.getKey( adminVO ) ); //admin
		 *
		 * </listing>
		 *
		 * @param  the key in which to retrieve the value of
		 * @return the value of the specified key
		 *
		 */
		public function getKey(value:*):*
		{
			var id:String=null;

			for (var key:*in dict)
			{
				if (dict[key] == value)
				{
					id=key;
					break;
				}
			}
			return id;
		}

		/**
		 *
		 * Returns each key added to the HashMap instance
		 *
		 * @example
		 *
		 * <listing version="3.0">
		 *
		 * import com.lombardrisk.utils.DictCache;
		 * import com.lombardrisk.utils.ICache;
		 *
		 * var cache:ICache = new DictCache();
		 * cache.put( "admin", adminVO );
		 * cache.put( "editor", editorVO );
		 *
		 * trace( cache.getKeys() ); //admin, editor
		 *
		 * </listing>
		 *
		 * @return Array of key identifiers
		 *
		 */
		public function getKeys():Array
		{
			var keys:Array=[];

			for (var key:* in dict)
			{
				keys.push(key);
			}
			return keys;
		}

		/**
		 *
		 * Retrieves the value of the specified key from the DictCache instance
		 *
		 * @example
		 * <listing version="3.0">
		 *
		 * import com.lombardrisk.utils.DictCache;
		 * import com.lombardrisk.utils.ICache;
		 *
		 * var cache:ICache = new DictCache();
		 * cache.put( "admin", adminVO );
		 * cache.put( "editor", editorVO );
		 *
		 * trace( cache.getValue( "editor" ) ); //[object, editorVO]
		 *
		 * </listing>
		 *
		 * @param  the key in which to retrieve the value of
		 * @return the value of the specified key, otherwise returns undefined
		 *
		 */
		public function getValue(key:*):*
		{
			return dict[key];
		}

		/**
		 *
		 * Retrieves each value assigned to each key in the DictCache instance
		 *
		 * @example
		 * <listing version="3.0">
		 *
		 * import com.lombardrisk.utils.DictCache;
		 * import com.lombardrisk.utils.ICache;
		 *
		 * var cache:ICache = new DictCache();
		 * cache.put( "admin", adminVO );
		 * cache.put( "editor", editorVO );
		 *
		 * trace( cache.getValues() ); //[object, adminVO],[object, editorVO]
		 *
		 * </listing>
		 *
		 * @return Array of values assigned for all keys in the cache
		 *
		 */
		public function getValues():Array
		{
			var values:Array=[];

			for (var key:* in dict)
			{
				values.push(dict[key]);
			}
			return values;
		}

		/**
		 *
		 * Determines the size of the HashMap instance
		 *
		 * @example
		 * <listing version="3.0">
		 *
		 * import com.lombardrisk.utils.DictCache;
		 * import com.lombardrisk.utils.ICache;
		 *
		 * var cache:ICache = new DictCache();
		 * cache.put( "admin", adminVO );
		 * cache.put( "editor", editorVO );
		 *
		 * trace( cache.size() ); //2
		 *
		 * </listing>
		 *
		 * @return the current size of the cache instance
		 *
		 */
		public function size():int
		{
			var length:int=0;

			for (var key:*in dict)
			{
				length++;
			}
			return length;
		}

		/**
		 *
		 * Determines if the current DictCache instance is empty
		 *
		 * @example
		 * <listing version="3.0">
		 *
		 * import com.lombardrisk.utils.DictCache;
		 * import com.lombardrisk.utils.ICache;
		 *
		 * var map:IMap = new HashMap();
		 * trace( map.isEmpty() ); //true
		 *
		 * map.put( "admin", adminVO );
		 * trace( map.isEmpty() ); //false
		 *
		 * </listing>
		 *
		 * @return true if the current map is empty, false if not
		 *
		 */
		public function isEmpty():Boolean
		{
			return size() <= 0;
		}

		/**
		 *
		 * Resets all key value assignments in the DictCache instance to null
		 *
		 * @example
		 * <listing version="3.0">
		 *
		 * import com.lombardrisk.utils.DictCache;
		 * import com.lombardrisk.utils.ICache;
		 *
		 * var cache:ICache = new DictCache();
		 * cache.put( "admin", adminVO );
		 * cache.put( "editor", editorVO );
		 * cache.reset();
		 *
		 * trace( cache.getValues() ); //null, null
		 *
		 * </listing>
		 *
		 */
		public function reset():void
		{
			for (var key:* in dict)
			{
				dict[key]=undefined;
			}
		}

		/**
		 *
		 * Resets all key / values defined in the DictCache instance to null
		 * with the exception of the specified key
		 *
		 * @example
		 * <listing version="3.0">
		 *
		 * import com.lombardrisk.utils.DictCache;
		 * import com.lombardrisk.utils.ICache;
		 *
		 * var cache:ICache = new DictCache();
		 * cache.put( "admin", adminVO );
		 * cache.put( "editor", editorVO );
		 *
		 * trace( cache.getValues() ); //[object, adminVO],[object, editorVO]
		 *
		 * cache.resetAllExcept( "editor", editorVO );
		 * trace( cache.getValues() ); //null,[object, editorVO]
		 *
		 * </listing>
		 *
		 * @param the key which is not to be cleared from the cache
		 *
		 */
		public function resetAllExcept(keyId:*):void
		{
			for (var key:* in dict)
			{
				if (key != keyId)
				{
					dict[key]=undefined;
				}
			}
		}

		/**
		 *
		 * Resets all key / values in the DictCache instance to null
		 *
		 * @example
		 * <listing version="3.0">
		 *
		 * import com.lombardrisk.utils.DictCache;
		 * import com.lombardrisk.utils.ICache;
		 *
		 * var cache:ICache = new DictCache();
		 * cache.put( "admin", adminVO );
		 * cache.put( "editor", editorVO );
		 * trace( cache.size() ); //2
		 *
		 * cache.clear();
		 * trace( cache.size() ); //0
		 *
		 * </listing>
		 *
		 */
		public function clear():void
		{
			for (var key:* in dict)
			{
				remove(key);
			}
		}

		/**
		 *
		 * Clears all key / values defined in the DictCache instance
		 * with the exception of the specified key
		 *
		 * @example
		 * <listing version="3.0">
		 *
		 * import com.lombardrisk.utils.DictCache;
		 * import com.lombardrisk.utils.ICache;
		 *
		 * var cache:ICache = new DictCache();
		 * cache.put( "admin", adminVO );
		 * cache.put( "editor", editorVO );
		 * trace( cache.size() ); //2
		 *
		 * cache.clearAllExcept( "editor", editorVO );
		 * trace( cache.getValues() ); //[object, editorVO]
		 * trace( cache.size() ); //1
		 *
		 * </listing>
		 *
		 * @param the key which is not to be cleared from the cache
		 *
		 */
		public function clearAllExcept(keyId:*):void
		{
			for (var key:* in dict)
			{
				if (key != keyId)
				{
					remove(key);
				}
			}
		}
	}
}
 ICache 类:

package
{
	import flash.utils.Dictionary;
	
	/**
     * 
     * Defines the contract for lightweight Cache implementations 
     * which are to expose an API into a managed collection of key 
     * value pairs
     * 
     */
	public interface ICache
	{
		 /**
         * 
         * Adds a key / value pair to the current cache
         * 
         * @param the key to add to the cache
         * @param the value of the specified key
         * 
         */
        function put(key:*, value:*) : void;
        
        /**
         *
         * Places all name / value pairs into the current
         * <code>ICache</code> instance.
         *  
         * @param an <code>Object</code> of name / value pairs
         * 
         */        
        function putAll(table:Dictionary) : void;        
         
        /**
         * 
         * Removes a key / value from the ICache instance
         *  
         * @param  key to remove from the cache
         * 
         */
        function remove(key:*) : void;

        /**
         * 
         * Determines if a key exists in the HashMap instance
         * 
         * @param  the key in which to determine existance in the map
         * @return true if the key exisits, false if not
         * 
         */
        function containsKey(key:*) : Boolean;

        /**
         * 
         * Determines if a value exists in the ICache instance
         * 
         * @param  the value in which to determine existance in the cache
         * @return true if the value exisits, false if not
         * 
         */
        function containsValue(value:*) : Boolean;

        /**
         * 
         * Returns a key value from the ICache instance
         * 
         * @param  the key in which to retrieve the value of
         * @return the value of the specified key
         * 
         */
        function getKey(value:*) : *;

        /**
         * 
         * Returns a key value from the ICache instance
         * 
         * @param  the key in which to retrieve the value of
         * @return the value of the specified key
         * 
         */
        function getValue(key:*) : *;

        /**
         * 
         * Returns each key added to the ICache instance
         * 
         * @return String Array of key identifiers
         * 
         */
        function getKeys() : Array;

        /**
         * 
         * Returns each value assigned to each key in the ICache instance
         * 
         * @return Array of values assigned for all keys in the cache
         * 
         */
        function getValues() : Array;
        
        /**
         * 
         * Retrieves the size of the ICache instance
         * 
         * @return the current size of the cache instance
         * 
         */
        function size() : int;

        /**
         * 
         * Determines if the HashMap instance is empty
         * 
         * @return true if the current map is empty, false if not
         * 
         */
        function isEmpty() : Boolean;
        
        /**
         * 
         * Resets all key value assignments in the ICache instance to null
         * 
         */
        function reset() : void;    
        
        /**
         * 
         * Resets all key / values defined in the ICache instance to null
         * 
         */
        function resetAllExcept(key:*) : void;    
                
        /**
         * 
         * Clears all key / values defined in the ICache instance
         * 
         */
        function clear() : void;

        /**
         * 
         * Clears all key / values defined in the ICache instance
         * with the exception of the specified key
         * 
         */
        function clearAllExcept(key:*) : void;
       
	}
}
 



 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值