WeakSet
object lets you store weakly held
objects
in a collection.
WeakSet
objects are collections of objects. Just as with
Set
s, each object in a
WeakSet
may occur only once; all objects in a
WeakSet
's collection are unique.
The main differences to the
Set
object are:
WeakSets
are collections of
objects only
. They cannot contain arbitrary values of any type, as
Set
s can.
WeakSet
is
weak,
meaning references to objects in a
WeakSet
are held
weakly
. If no other references to an object stored in the
WeakSet
exist, those objects can be garbage collected.
注意:
This also means that there is no list of current objects stored in the collection.
WeakSets
are not enumerable.
Functions that call themselves recursively need a way of guarding against circular data structures by tracking which objects have already been processed.
WeakSet
s are ideal for this purpose:
// Execute a callback on everything stored inside an object
function execRecursively(fn, subject, _refs = null){
if(!_refs)
_refs = new WeakSet();
// Avoid infinite recursion
if(_refs.has(subject))
return;
fn(subject);
if("object" === typeof subject){
_refs.add(subject);
for(let key in subject)
execRecursively(fn, subject[key], _refs);
}
}
const foo = {
foo: "Foo",
bar: {
bar: "Bar"
}
};
foo.bar.baz = foo; // Circular reference!
execRecursively(obj => console.log(obj), foo);
Here, a
WeakSet
is created on the first run, and passed along with every subsequent function call (using the internal
_refs
parameter).
The number of objects or their traversal order is immaterial, so a
WeakSet
is more suitable (and performant) than a
Set
for tracking object references, especially if a very large number of objects is involved.
WeakSet()
WeakSet
对象。
WeakSet.prototype.add(
value
)
value
到
WeakSet
对象。
WeakSet.prototype.delete(
value
)
value
从
WeakSet
.
WeakSet.prototype.has(
value
)
will return
false
afterwards.
WeakSet.prototype.has(
value
)
value
is present in the
WeakSet
object or not.
const ws = new WeakSet();
const foo = {};
const bar = {};
ws.add(foo);
ws.add(bar);
ws.has(foo); // true
ws.has(bar); // true
ws.delete(foo); // removes foo from the set
ws.has(foo); // false, foo has been removed
ws.has(bar); // true, bar is retained
注意,
foo !== bar
. While they are similar objects,
they are not
the same object
. And so they are both added to the set.
| 规范 |
|---|
|
ECMAScript (ECMA-262)
The definition of 'WeakSet' in that specification. |
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
WeakSet
|
Chrome 36 | Edge 12 | Firefox 34 | IE No | Opera 23 | Safari 9 | WebView Android 37 | Chrome Android 36 | Firefox Android 34 | Opera Android 24 | Safari iOS 9 | Samsung Internet Android 3.0 | nodejs 0.12 |
WeakSet()
构造函数
|
Chrome 36 | Edge 12 | Firefox 34 | IE No | Opera 23 | Safari 9 | WebView Android 37 | Chrome Android 36 | Firefox Android 34 | Opera Android 24 | Safari iOS 9 | Samsung Internet Android 3.0 | nodejs 0.12 |
add
|
Chrome 36 | Edge 12 | Firefox 34 | IE No | Opera 23 | Safari 9 | WebView Android 37 | Chrome Android 36 | Firefox Android 34 | Opera Android 24 | Safari iOS 9 | Samsung Internet Android 3.0 | nodejs 0.12 |
clear
弃用
非标
|
Chrome 36 — 43 | Edge No | Firefox 34 — 46 | IE No | Opera 25 — 30 | Safari No | WebView Android 37 — 43 | Chrome Android 36 — 43 | Firefox Android 34 — 46 | Opera Android 25 — 30 | Safari iOS No | Samsung Internet Android 3.0 — 4.0 | nodejs No |
delete
|
Chrome 36 | Edge 12 | Firefox 34 | IE No | Opera 23 | Safari 9 | WebView Android 37 | Chrome Android 36 | Firefox Android 34 | Opera Android 24 | Safari iOS 9 | Samsung Internet Android 3.0 | nodejs 0.12 |
has
|
Chrome 36 | Edge 12 | Firefox 34 | IE No | Opera 23 | Safari 9 | WebView Android 37 | Chrome Android 36 | Firefox Android 34 | Opera Android 24 | Safari iOS 9 | Samsung Internet Android 3.0 | nodejs 0.12 |
完整支持
不支持
非标。预期跨浏览器支持较差。
弃用。不要用于新网站。
WeakSet
Function
Object
Object.prototype.__defineGetter__()
Object.prototype.__defineSetter__()
Object.prototype.__lookupGetter__()
Object.prototype.__lookupSetter__()
Object.prototype.hasOwnProperty()
Object.prototype.isPrototypeOf()
Object.prototype.propertyIsEnumerable()
Object.prototype.toLocaleString()
Object.prototype.toSource()
Object.prototype.toString()
Object.prototype.valueOf()
Object.setPrototypeOf()