Warning: This is obsolete - new code should use Map()
.
The Dict.jsm
JavaScript code module offers routines for managing dictionaries of key/value pairs. To use it, you first need to import the code module into your JavaScript scope:
Components.utils.import("resource://gre/modules/Dict.jsm");
创建一个字典
You can create a new, empty dictionary by simply calling the Dict()
constructor:
var newDict = new Dict();
If you wish, you may also pass in an object literal of key/value pairs with which to initialize the dictionary:
var someObj = {}; var newDict = new Dict({key1: "foo", key2: someObj});
Note that values may be any JavaScript object type.
从Firefox 19开始,你可以通过传入一个JSON字符串来初始化字典对象:
var someJSON = '{key1: "foo", key2: {}}'; var newDict = new Dict(someJSON);
方法概述
Dict copy(); |
boolean del(String aKey); |
Object get( String aKey , [optional] Object aDefault); |
boolean has( String aKey ); |
Array listitems(); |
Array listkeys(); |
Array listvalues(); |
void set(String aKey , Object aValue); |
String toJSON(); |
String toString(); |
属性
属性名 | 类型 | 描述 |
count |
Number |
字典中键值对的个数 |
items |
Iterator |
Returns an iterator over all of the items in the dictionary; each item is returned as a pair (a two-element array) with the first element being the key and the second being the value. Note: The order in which items are returned is arbitrary, and may change without notice. In addition, if the dictionary changes during iteration, no guarantees are made as to what will happen.
|
keys |
Iterator |
Returns an iterator over all the keys in the dictionary. Note: The order in which items are returned is arbitrary, and may change without notice. In addition, if the dictionary changes during iteration, no guarantees are made as to what will happen.
|
values |
Iterator |
Returns an iterator over all the values in the dictionary. Note: The order in which items are returned is arbitrary, and may change without notice. In addition, if the dictionary changes during iteration, no guarantees are made as to what will happen.
|
构造器
Dict()
创建并返回一个新的字典对象.
Dict Dict(); Dict Dict( Object initalKeysAndValues );
参数
initialKeysAndValues
可选- A object containing key/value pairs with which to initialize the dictionary.
返回值
一个新的字典对象,实现有下面这些方法.
方法
copy()
返回一个字典对象的浅拷贝; that is, a copy of the dictionary including the items immediately included within the dictionary; however, any objects referenced by those top-level objects are not copied.
Dict copy();
参数
无
返回值
A new dictionary object containing the same top-level items as the original dictionary on which the copy()
method was called.
del()
根据指定的键,从该字典中删除一个键值对.
boolean del( String aKey );
参数
aKey
- 从该字典中要删除的键.
返回值
如果成功删除指定的键值对,则返回true
,如果指定的键不存在,则返回false
.
get()
返回该字典对象中指定键所对应的值.
Object get( String
aKey
, [optional] Object aDefault );
参数
aKey
- The key whose value should be returned.
aDefault
可选- The value to return if the specified key isn't found. If you don't specify a default value,
undefined
is returned for keys that aren't found.
返回值
The value of the specified key, or undefined
if no matching key was found.
has()
判断指定的键是否存在与当前字典对象中.
boolean has( String
aKey
);
参数
aKey
- 判断该键是否存在与当前字典对象中..
返回值
如果指定的键存在与当前字典中,则返回true
,否则返回false
.
listitems()
返回一个由当前字典中所有键值对组成的数组.
Array listitems();
参数
无
返回值
一个由当前字典中所有键值对组成的数组.
listkeys()
返回一个由当前字典中所有键组成的数组.
Array listkeys();
参数
无
返回值
一个由当前字典中所有键组成的数组.
listvalues()
返回一个由当前字典中所有值组成的数组.
Array listvalues();
参数
无
返回值
一个由当前字典中所有值组成的数组.
set()
设置指定键所对应的值,如果该键不存在,则添加上这个新键.
void set( String
aKey
, Object aValue );
参数
aKey
- 设置改建所对应的值.
aValue
- 为指定键所设置的新值.
toJSON()
返回一个代表当前字典的 JSON字符串.
String toJSON();
参数
无
返回值
返回一个代表了该字典对象中所有键值对的JSON字符串,空字典将返回"{}".
toString()
返回一个代表该字典对象的字符串.
String toString();
参数
无
返回值
返回一个代表了该字典对象中所有键值对的字符串,空字典将返回"{}".
示例
创建和填充字典
下面的例子首先创建一个新的空字典,然后添加进一些键值对.
var myDict = new Dict(); myDict.set("name", "John Smith"); myDict.set("email", "[email protected]"); myDict.set("phone", "650-555-1234");
判断字典中指定的键是否存在
接着上面的个例子,你可以检测某个键是否存在于myDict
中:
if (myDict.has("email")) { /* an "email" key exists on the object */ }