JavaScript参考手册
WeakSet
WeakSet
WeakSet
对象允许你将弱保持对象存储在一个集合中。
语法
new WeakSet([iterable]);
参数
iterable如果传入一个可迭代对象作为参数, 则该对象的所有迭代值都会被自动添加进生成的 WeakSet
对象中.
描述
WeakSet
对象是一些对象值的集合, 并且其中的每个对象值都只能出现一次.
它和 Set
对象的区别有两点:
-
WeakSet
对象中只能存放对象引用, 不能存放值, 而Set
对象都可以.
-
WeakSet
对象中存储的对象值都是被弱引用的, 如果没有其他的变量或属性引用这个对象值, 则这个对象值会被当成垃圾回收掉. 正因为这样,WeakSet
对象是无法被枚举的, 没有办法拿到它包含的所有元素.
属性
WeakSet.lengthlength
属性的值为 0.
WeakSet.prototype
WeakSet
实例的所有继承属性和继承方法都在该对象上.
WeakSet
实例
所有WeakSet
实例都继承自WeakSet.prototype
.
属性
WeakSet.prototype.constructor
返回构造函数即WeakSet
本身.
方法
WeakSet.prototype.add(value)
在该 WeakSet
对象中添加一个新元素value
.
WeakSet.prototype.clear()
清空该 WeakSet
对象中的所有元素.
WeakSet.prototype.delete(value)
从该 WeakSet
对象中删除value
这个元素, 之后 WeakSet.prototype.has(value)
方法便会返回false
.
WeakSet.prototype.has(value)
返回一个布尔值, 表示给定的值value
是否存在于这个 WeakSet
中.
示例
例1: 使用 WeakSet
var ws = new WeakSet();
var obj = {};
var foo = {};
ws.add(window);
ws.add(obj);
ws.has(window); // true
ws.has(foo); // false, foo has not been added to the set
ws.delete(window); // removes window from the set
ws.has(window); // false, window has been removed
规范
Specification |
Status |
Comment |
---|---|---|
ECMAScript 2015 (6th Edition, ECMA-262)The definition of 'WeakSet' in that specification. |
Standard |
Initial definition. |
ECMAScript Latest Draft (ECMA-262)The definition of 'WeakSet' in that specification. |
Living Standard |
|
浏览器兼容性
Feature |
Chrome |
Edge |
Firefox (Gecko) |
Internet Explorer |
Opera |
Safari |
---|---|---|---|---|---|---|
Basic support |
36 |
12 |
34 (34) |
No support |
23 |
9 |
new WeakSet(iterable) |
38 |
12 |
34 (34) |
No support |
25 |
9 |
Constructor argument: new WeakSet(null) |
(Yes) |
12 |
37 (37) |
No support |
? |
9 |
Obsolete clear() method removed |
43 |
12 |
46 (46) |
No support |
30 |
9 |
Feature |
Android |
Edge |
Firefox Mobile (Gecko) |
IE Mobile |
Opera Mobile |
Safari Mobile |
---|---|---|---|---|---|---|
Basic support |
No support |
(Yes) |
34.0 (34) |
No support |
No support |
9 |
new WeakMap(iterable) |
No support |
(Yes) |
34.0 (34) |
No support |
No support |
9 |
Constructor argument: new WeakSet(null) |
? |
(Yes) |
(Yes) |
No support |
? |
9 |
Obsolete clear() method removed |
No support |
(Yes) |
? |
No support |
? |
9 |
WeakSet | ||
---|---|---|
weakSet.add | 详细 | |
weakSet.delete | 详细 | |
weakSet.has | 详细 | |
WeakSet.prototype | 详细 |
JavaScript 是一种高级编程语言,通过解释执行,是一门动态类型,面向对象(基于原型)的解释型语言。它已经由ECMA(欧洲电脑制造商协会)通过 ECMAScript 实现语言的标准化。它被世界上的绝大多数网站所使用,也被世界主流浏览器( Chrome、IE、FireFox、Safari、Opera )支持。JavaScript 是一门基于原型、函数先行的语言,是一门多范式的语言,它支持面向对象编程,命令式编程,以及函数式编程。它提供语法来操控文本、数组、日期以及正则表达式等,不支持 I/O,比如网络