flash.utils

# Dictionary - AS3

Package | flash.utils
Class | public dynamic class Dictionary
Inheritance | Dictionary Object

Language Version: | ActionScript 3.0

Runtime Versions: | AIR 1.0, Flash Player 9, Flash Lite 4

The Dictionary class lets you create a dynamic collection of properties, which uses strict equality (`===`) for key comparison. When an object is used as a key, the object's identity is used to look up the object, and not the value returned from calling `toString()` on it.

Note: You cannot use a QName object as a Dictionary key.

The following statements show the relationship between a Dictionary object and a key object:

     var dict = new Dictionary();
     var obj = 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 b/c key.toString == "key"
     dict["key"] == "Letters"; // false because "key" === key is false
     delete dict[key]; //removes the key

Learn more

Associative arrays

Related API Elements

=== (strict equality)

Public Properties
Hide Inherited Public Properties
Show Inherited Public Properties

| Property | Defined By
--- | --- | ---
|  | constructor : Object A reference to the class object or constructor function for a given object instance. | Object

Public Methods
Hide Inherited Public Methods
Show Inherited Public Methods

| Method | Defined By
--- | --- | ---
|  | Dictionary(weakKeys:Boolean = false) Creates a new Dictionary object. | Dictionary
|  | hasOwnProperty(name:String):Boolean Indicates whether an object has a specified property defined. | Object
|  | isPrototypeOf(theClass:Object):Boolean Indicates whether an instance of the Object class is in the prototype chain of the object specified as the parameter. | Object
|  | propertyIsEnumerable(name:String):Boolean Indicates whether the specified property exists and is enumerable. | Object
|  | setPropertyIsEnumerable(name:String, isEnum:Boolean = true):void Sets the availability of a dynamic property for loop operations. | Object
|  | toJSON(k:String):* Provides an overridable method for customizing the JSON encoding of values in an Dictionary object. | Dictionary
|  | toLocaleString():String Returns the string representation of this object, formatted according to locale-specific conventions. | Object
|  | toString():String Returns the string representation of the specified object. | Object
|  | valueOf():Object Returns the primitive value of the specified object. | Object

Constructor Detail

### Dictionary

()
Constructor

`public function Dictionary(weakKeys:Boolean = false)`

Language Version: | ActionScript 3.0

Runtime Versions: | AIR 1.0, Flash Player 9, Flash Lite 4

Creates a new Dictionary object. To remove a key from a Dictionary object, use the `delete` operator.

Parameters

| `weakKeys:Boolean` (default = `false`)`` — Instructs the Dictionary object to use "weak" references on object keys. 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. Note that the Dictionary never removes weak `String` keys from the table. Specifically in the case of `String` keys, the weak reference will never get removed from the key table, and the Dictionary will keep holding a strong reference to the respective values.

Method Detail

### toJSON

()
method

`public function toJSON(k:String):*`

Language Version: | ActionScript 3.0

Runtime Versions: | Flash Player 11, AIR 3, Flash Lite 4

Provides an overridable method for customizing the JSON encoding of values in an Dictionary object.

The `JSON.stringify()` method looks for a `toJSON()` method on each object that it traverses. If the `toJSON()` method is found, `JSON.stringify()` calls it for each value it encounters, passing in the key that is paired with the value.

Dictionary provides a default implementation of `toJSON()` that simply returns the name of the class. Clients that wish to export Dictionary objects to JSON must provide their own `toJSON()` implementations. You can do so by redefining the `toJSON()` method on the class prototype.

The `toJSON()` method can return a value of any type. If it returns an object, `stringify()` recurses into that object. If `toJSON()` returns a string, `stringify()` does not recurse and continues its traversal.

Parameters

| `k:String` — The key of a key/value pair that `JSON.stringify()` has encountered in its traversal of this object

Returns

| `*` — The class name string.

Learn more

Overview of the JSON API

Related API Elements

Object
Object.prototype

© 2015 Adobe Systems Incorporated. All rights reserved.
Mon Apr 10 2017, 12:28 PM -07:00