Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Understanding the Differences Between Object and Dictionary in ActionScript 3

Tech 1

The Dictionary class in ActionScript 3 (flash.utils.Dictionary) introduces a key distinction from the traditional Object class: it allows keys of any data type, not just strings.

When using Object instances as associative arrays, all keys are automatically converted to strings. This conversion can lead to unexpected behavior:

var collection:Object = new Object();
collection["identifier"] = 100;  // String key "identifier"
collection[42] = 200;             // Numeric key 42 becomes string "42"
collection[new Object()] = 300;   // Object key becomes "[object Object]"

for (var key:String in collection) {
    trace(key);          // Outputs: [object Object], 42, identifier
    trace(collection[key]); // Outputs: 300, 200, 100
}

This string conversion means that different objects used as keys will all resolve to the same string representation "[object Object]", causing them to reference the same value:

var firstKey:Object = new Object();
var secondKey:Object = new Object();

var data:Object = new Object();
data[firstKey] = "First Value";   // Actually sets data["[object Object]"]
data[secondKey] = "Second Value"; // Overwrites data["[object Object]"]

for (var item:String in data) {
    trace(item);       // Outputs: [object Object]
    trace(data[item]); // Outputs: Second Value
}

The Dictionary class eliminates this limitation by preserving the actual object references as keys:

import flash.utils.Dictionary;

var keyA:Object = new Object();
var keyB:Object = new Object();

var mapping:Dictionary = new Dictionary();
mapping[keyA] = "Value A";
mapping[keyB] = "Value B";

for (var entry:* in mapping) {
    trace(entry);        // Outputs: [object Object], [object Object]
    trace(mapping[entry]); // Outputs: Value A, Value B
}

Important considerations when working with Dictionary:

  1. Although trace output still shows "[object Object]" for object keys, these represent distinct object referances within the Dictionary.
  2. The iteration variable must be declared with the * type annotation since Dictionary keys can be of any data type.

Key differences between Object and Dictionayr:

  1. Object keys are always converted to strings via toString() if they are not already string values.
  2. Dictionary keys maintain object reference integrity and use strict equality (===) comparison without type coercion for key lookup operations.

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.