WeakSet

WeakSet 对象允许你将弱保持对象存储在一个集合中。

语法

 new WeakSet([iterable]);

参数

iterable
如果传入一个可迭代对象作为参数, 则该对象的所有迭代值都会被自动添加进生成的 WeakSet 对象中。null 被认为是 undefined。

示例

使用 WeakSet对象

var ws = new WeakSet();
var foo = {};
var bar = {};

ws.add(foo);
ws.add(bar);

ws.has(foo);    // true
ws.has(bar);   // true

ws.delete(foo); // 从set中删除 foo 对象
ws.has(foo);    // false, foo 对象已经被删除了
ws.has(bar);    // true, bar 依然存在

注意, foo  !==  bar。 尽管它们是相似的对象,但是它们不是同一个对象。因此,它们都可以被加入到set中。

描述

WeakSet 对象是一些对象值的集合, 并且其中的每个对象值都只能出现一次。在WeakSet的集合中是唯一的

它和 Set 对象的区别有两点:

  • Set相比,WeakSet 只能是对象的集合,而不能是任何类型的任意值。
  • WeakSet持弱引用:集合中对象的引用为弱引用。 如果没有其他的对WeakSet中对象的引用,那么这些对象会被当成垃圾回收掉。 这也意味着WeakSet中没有存储当前对象的列表。 正因为这样,WeakSet 是不可枚举的。

检测循环引用

递归调用自身的函数需要一种通过跟踪哪些对象已被处理,来应对循环数据结构的方法。

为此,WeakSet非常适合处理这种情况:

// 对 传入的subject对象 内部存储的所有内容执行回调
function execRecursively(fn, subject, _refs = null){
	if(!_refs)
		_refs = new WeakSet();
	
	// 避免无限递归
	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; // 循环引用!
execRecursively(obj => console.log(obj), foo);

在此,在第一次运行时创建WeakSet,并将其与每个后续函数调用一起传递(使用内部参数_refs)。 对象的数量或它们的遍历顺序无关紧要,因此,WeakSet比Set更适合(和执行)跟踪对象引用,尤其是在涉及大量对象时。

属性

WeakSet.length
length 属性的值为 0.
WeakSet.prototype
表示WeakSet构造函数的原型。 允许向所有WeakSet对象添加属性。

WeakSet 实例

所有 WeakSet 实例都继承自 WeakSet.prototype.

属性

WeakSet.prototype.constructor
返回构造函数即 WeakSet 本身.

方法

WeakSet.prototype.add(value)
 在该 WeakSet 对象中添加一个新元素 value.
WeakSet.prototype.delete(value)
该 WeakSet 对象中删除 value 这个元素, 之后 WeakSet.prototype.has(value) 方法便会返回 false.
WeakSet.prototype.has(value)
返回一个布尔值,  表示给定的值 value 是否存在于这个 WeakSet 中.

规范

规范链接 规范状态 备注
ECMAScript 2015 (6th Edition, ECMA-262)
WeakSet
Standard Initial definition.
ECMAScript Latest Draft (ECMA-262)
WeakSet
Draft

浏览器兼容性

Update compatibility data on GitHub
DesktopMobileServer
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung InternetNode.js
WeakSetChrome Full support 36Edge Full support 12Firefox Full support 34IE No support NoOpera Full support 23Safari Full support 9WebView Android Full support 37Chrome Android Full support 36Firefox Android Full support 34Opera Android Full support 24Safari iOS Full support 9Samsung Internet Android Full support 3.0nodejs Full support 0.12
addChrome Full support 36Edge Full support 12Firefox Full support 34IE No support NoOpera Full support 23Safari Full support 9WebView Android Full support 37Chrome Android Full support 36Firefox Android Full support 34Opera Android Full support 24Safari iOS Full support 9Samsung Internet Android Full support 3.0nodejs Full support 0.12
clear
DeprecatedNon-standard
Chrome No support 36 — 43Edge No support NoFirefox No support 34 — 46IE No support NoOpera No support 25 — 30Safari No support NoWebView Android No support 37 — 43Chrome Android No support 36 — 43Firefox Android No support 34 — 46Opera Android No support 25 — 30Safari iOS No support NoSamsung Internet Android No support 3.0 — 4.0nodejs No support No
deleteChrome Full support 36Edge Full support 12Firefox Full support 34IE No support NoOpera Full support 23Safari Full support 9WebView Android Full support 37Chrome Android Full support 36Firefox Android Full support 34Opera Android Full support 24Safari iOS Full support 9Samsung Internet Android Full support 3.0nodejs Full support 0.12
hasChrome Full support 36Edge Full support 12Firefox Full support 34IE No support NoOpera Full support 23Safari Full support 9WebView Android Full support 37Chrome Android Full support 36Firefox Android Full support 34Opera Android Full support 24Safari iOS Full support 9Samsung Internet Android Full support 3.0nodejs Full support 0.12
prototypeChrome Full support 36Edge Full support 12Firefox Full support 34IE No support NoOpera Full support 23Safari Full support 9WebView Android Full support 37Chrome Android Full support 36Firefox Android Full support 34Opera Android Full support 24Safari iOS Full support 9Samsung Internet Android Full support 3.0nodejs Full support 0.12
new WeakSet(iterable)Chrome Full support 38Edge Full support 12Firefox Full support 34IE No support NoOpera Full support 25Safari Full support 9WebView Android Full support 38Chrome Android Full support 38Firefox Android Full support 34Opera Android Full support 25Safari iOS Full support 9Samsung Internet Android Full support 3.0nodejs Full support 0.12
new WeakSet(null)Chrome Full support 36Edge Full support 12Firefox Full support 37IE No support NoOpera Full support 23Safari Full support 9WebView Android Full support 37Chrome Android Full support 36Firefox Android Full support 37Opera Android Full support 24Safari iOS Full support 9Samsung Internet Android Full support 3.0nodejs Full support 0.12

Legend

Full support  
Full support
No support  
No support
Non-standard. Expect poor cross-browser support.
Non-standard. Expect poor cross-browser support.
Deprecated. Not for use in new websites.
Deprecated. Not for use in new websites.

相关链接