Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Dict.jsm

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.

Note: You can actually specify non-strings as keys; these are converted to strings before being used, so they're documented here as if they were a string parameter.

从Firefox 19开始,你可以通过传入一个JSON字符串来初始化字典对象:

var someJSON = '{key1: "foo", key2: {}}';
var newDict = new Dict(someJSON);
注: 传入的任意字符串都会被当成JSON字符串来对待.

方法概述

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 */
} 

文档标签和贡献者

 此页面的贡献者: tiansh, ziyunfei
 最后编辑者: tiansh,