forked from nkronlage/JavaScripture
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweakmap.jsdoc
More file actions
167 lines (120 loc) · 3.76 KB
/
weakmap.jsdoc
File metadata and controls
167 lines (120 loc) · 3.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
WeakMap : Object
WeakMaps allow associating keys and values similar to %%/Map|Map%%
except WeakMap does not allow iterating over its keys or values.
If WeakMap would be the only object holding on to the key/value
pair, the pair will be released from memory.
The keys cannot be primitive values
(%%/Boolean|Boolean%%,
%%/Number|Number%%,
%%/String|String%%,
or
%%/undefined|undefined%%).
Version:
ECMAScript 2015
Spec:
http://www.ecma-international.org/ecma-262/6.0/#sec-weakmap-objects
----
new WeakMap() : WeakMap
Creates an empty WeakMap.
<example>
var key1 = { foo: 1 };
var key2 = { bar: ['a', 'b', 'c'] };
var x = new WeakMap([[key1, 'value 1'], [key2, 'value 2']]);
console.log(x.get(key1));
console.log(x.get(key2));
</example>
Spec:
http://www.ecma-international.org/ecma-262/6.0/#sec-weakmap-iterable
----
new WeakMap(iterable : Object) : WeakMap
Creates a WeakMap by iterating over **iterable** and using
the first element of the iterator value as the key and
second element as the value.
<example>
var key1 = { foo: 1 };
var key2 = { bar: ['a', 'b', 'c'] };
var fromArray = new WeakMap([[key1, 'array0'], [key2, 'array1']]);
console.log(fromArray.get(key1));
console.log(fromArray.get(key2));
console.log();
var generator = function*() {
yield [key1, 'generator0'];
yield [key2, 'generator1'];
};
var fromGenerator = new WeakMap(generator());
console.log(fromGenerator.get(key1));
console.log(fromGenerator.get(key2));
</example>
Spec:
http://www.ecma-international.org/ecma-262/6.0/#sec-weakmap-iterable
----
prototype.delete(key : Object) : Boolean
Removes **key** and its corresponding value from **this**.
Returns **true** if **key** was in **this** before deleting it.
<example>
var key1 = { foo: 1 };
var key2 = { bar: ['a', 'b', 'c'] };
var x = new WeakMap([[key1, 'value 1'], [key2, 'value 2']]);
console.log(x.get(key1));
console.log(x.get(key2));
x.delete(key1);
console.log(x.get(key1));
console.log(x.get(key2));
</example>
Spec:
http://www.ecma-international.org/ecma-262/6.0/#sec-weakmap.prototype.delete
----
prototype.get(key : Object) : Object
Returns the value stored for **key**. If no value is stored,
returns %%/undefined|**undefined**%%.
See also %%#has|**has()**%% and %%#set|**set()**%%.
<example>
var key1 = { foo: 1 };
var key2 = { bar: ['a', 'b', 'c'] };
var x = new WeakMap([[key1, 'value 1'], [key2, 'value 2']]);
console.log(x.get(key1));
console.log(x.get(key2));
// get returns undefined for keys not in the WeakMap
var key3 = { language: 'JavaScript' };
console.log(x.get(key3));
</example>
Spec:
http://www.ecma-international.org/ecma-262/6.0/#sec-weakmap.prototype.get
----
prototype.has(key : Object) : Boolean
Returns **true** if the map has a value stored for **key**.
<example>
var key1 = { foo: 1 };
var key2 = { bar: ['a', 'b', 'c'] };
var x = new WeakMap();
x.set(key1, undefined);
// has() lets you check if a value is stored, even if
// the value is undefined
console.log(x.has(key1) + ' ' + x.get(key1));
console.log(x.has(key2) + ' ' + x.get(key2));
</example>
Spec:
http://www.ecma-international.org/ecma-262/6.0/#sec-weakmap.prototype.has
----
prototype.set(key : Object, value : Object) : WeakMap
Stores **value** in **this** at the specified **key**.
**key** cannot be a primitive value
(%%/Boolean|Boolean%%,
%%/Number|Number%%,
%%/String|String%%,
or
%%/undefined|undefined%%).
If a value is already stored for that **key**, it is replaced with
**value**.
Returns **this**.
See also %%#get|**get()**%% and %%#has|**has()**%%.
<example>
var key1 = { foo: 1 };
var key2 = { bar: ['a', 'b', 'c'] };
var x = new WeakMap([[key1, 'value 1'], [key2, 'value 2']]);
console.log(x.get(key1));
x.set(key1, 'a different value');
console.log(x.get(key1));
</example>
Spec:
http://www.ecma-international.org/ecma-262/6.0/#sec-weakmap.prototype.set