Dictionary
While the Dictionary object is very helpful to VBScript authors, JavaScript already provides the equivalent functionality natively. A Dictionary object behaves very much like a JavaScript array that has string index values (similar to a Java hash table), although numeric index values are also acceptable in the Dictionary. Indexes are called keys in this environment. VBScript arrays do not have this facility natively, so the Dictionary object supplements the language for the sake of convenience. Unlike a JavaScript array, however, you must use the various properties and methods of the Dictionary object to add, access, or remove items from it.
You create a Dictionary object via ActiveXObject as follows:
var dict = new ActiveXObject("Scripting.Dictionary")
You must create a separate Dictionary object for each array. Table 42-2 lists the properties and methods of the Dictionary object. After you create a blank Dictionary object, populate it via the Add() method for each entry. For example, the following statements create a Dictionary object to store U.S. state capitals:
var stateCaps = new ActiveXObject("Scripting.Dictionary") stateCaps.Add("Illinois", "Springfield")
You can then access an individual item via the Key property (which, thanks to its VBScript heritage, looks more like a JavaScript method). One convenience of the Dictionary object is the Keys() method, which returns an array of all the keys in the dictionary — something that a string-indexed JavaScript array could use.
Post a comment