A dictionary is a special kind of collection or array which allows you to access items using name and value pairs. This is functionally similar to using associative name indexing on a JavaScript array, so although this is not generally available, its fundamental usefulness is already provided in the native JavaScript implementation.
The Dictionary object provided by JScript and Active X, has a few extra properties and methods to help manage the contents of the dictionary. These could be simulated fairly easily and added to the Array prototype to create your own portable Dictionary-like object.
As this object needs to be created with the Active X facilities, it is not supported outside of the Windows environment. It is odd that such a useful object can only be constructed in this way and that it does not have a native JavaScript constructor. That at least would make it available in all platforms that JScript runs in.
Note that the properties and methods for the Dictionary object all start with an upper case letter. This is not the common practice in JavaScript and may cause you a few run-time errors if you forget.
There are some unusual syntactical constructs in this object. In particular, item and key methods that behave like properties. These are intended to provide an interface to replace an item in the dictionary or to rename a key. These methods would normally be made with several arguments to a single method, but instead they use a method to retrieve a reference to a Dictionary pocket and you can then assign a value to the reference that was returned.
// Create a new dictionary var myDictionary = new ActiveXObject("Scripting.Dictionary"); // Store some items in the dictionary // (Melting Points of fats/waxes) myDictionary.Add("Butter", "28"); myDictionary.Add("Lard", "36"); myDictionary.Add("MuttonTallow", "44"); myDictionary.Add("Beeswax", "61"); myDictionary.Add("Stearin", "71.6"); myDictionary.Add("ParaffinWax", "38"); // Display one item if it exists if(myDictionary.Exists("ParaffinWax")) { document.write("Paraffin Wax melts at "); document.write(myDictionary.Item("ParaffinWax")); document.write(" degrees Centigrade."); } // Remove an item if(myDictionary.Exists("Stearin")) { myDictionary.Remove("Stearin"); } // Change an item and its key if(myDictionary.Exists("MuttonTallow")) { myDictionary.Item("MuttonTallow") = "40"; myDictionary.Key("MuttonTallow") = "BeefTallow"; } // List all the items myArray = (new VBArray(myDictionary.Keys())).toArray(); for(myEnum=0; myEnum<myArray.length; myEnum++) { document.write("Key value: "); document.write(myArray[myEnum]); document.write("Item value: "); document.write(myDictionary.Item(myArray[myEnum])); document.write("<BR>"); } // Now discard the items in the array myDictionary.RemoveAll();
See also: | ActiveX, ActiveXObject object |
Property | JavaScript | JScript | N | IE | Opera | NES | ECMA | DOM | CSS | HTML | Notes |
---|---|---|---|---|---|---|---|---|---|---|---|
Count | ![]() | 3.0 ![]() | ![]() | 4.0 ![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ReadOnly |
Prev | Home | Next |
dialogWidth | Up | Dictionary.Add() |
JavaScript Programmer's Reference, Cliff Wootton Wrox Press (www.wrox.com) Join the Wrox JavaScript forum at p2p.wrox.com Please report problems to support@wrox.com © 2001 Wrox Press. All Rights Reserved. Terms and conditions. |