ECMAScript 6:WeakSet 有什么用? [英] ECMAScript 6: what is WeakSet for?

查看:21
本文介绍了ECMAScript 6:WeakSet 有什么用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

WeakSet 应该通过弱引用来存储元素.也就是说,如果一个对象没有被其他任何东西引用,它应该从 WeakSet 中清除.

The WeakSet is supposed to store elements by weak reference. That is, if an object is not referenced by anything else, it should be cleaned from the WeakSet.

我编写了以下测试:

var weakset = new WeakSet(),
    numbers = [1, 2, 3];

weakset.add(numbers);
weakset.add({name: "Charlie"});

console.log(weakset);

numbers = undefined;

console.log(weakset);

即使我的 [1, 2, 3] 数组没有被任何东西引用,它也不会从 WeakSet 中删除.控制台打印:

Even though my [1, 2, 3] array is not referenced by anything, it's not being removed from the WeakSet. The console prints:

WeakSet {[1, 2, 3], Object {name: "Charlie"}}
WeakSet {[1, 2, 3], Object {name: "Charlie"}}

这是为什么?

另外,我还有一个问题.直接向 WeakSets 添加对象有什么意义,像这样:

Plus, I have one more question. What is the point of adding objects to WeakSets directly, like this:

weakset.add({name: "Charlie"});

是那些 Traceur 的小故障还是我遗漏了什么?

Are those Traceur's glitches or am I missing something?

最后,如果我们甚至无法遍历它也无法获得当前大小,那么 WeakSet 的实际用途是什么?

And finally, what is the practical use of WeakSet if we cannot even iterate through it nor get the current size?

推荐答案

它不会从 WeakSet 中删除.这是为什么?

it's not being removed from the WeakSet. Why is that?

很可能是因为垃圾收集器尚未运行.但是,您说您正在使用 Traceur,所以可能是因为它们没有得到适当的支持.我想知道 console 如何显示 WeakSet 的内容.

Most likely because the garbage collector has not yet run. However, you say you are using Traceur, so it just might be that they're not properly supported. I wonder how the console can show the contents of a WeakSet anyway.

直接向 WeakSet 添加对象有什么意义?

What is the point of adding objects to WeakSets directly?

WeakSet 添加对象字面量绝对没有意义.

There is absolutely no point of adding object literals to WeakSets.

如果我们甚至无法遍历它也无法获得当前大小,那么 WeakSet 的实际用途是什么?

What is the practical use of WeakSet if we cannot even iterate through it nor get the current size?

你所能得到的只是一点信息:对象(或一般来说,值)是否包含在集合中?

All you can get is one bit of information: Is the object (or generically, value) contained in the set?

这在您想要标记"对象而不实际改变它们(在它们上设置属性)的情况下很有用.许多算法都包含某种如果 x 已经被看到"条件(JSON.stringify 循环检测可能是一个很好的例子),当你与用户一起工作时——提供的值使用 Set/WeakSet 是可取的.WeakSet 的优点是它的内容可以在你的算法仍在运行时被垃圾收集,因此当你处理大量数据时它有助于减少内存消耗(甚至防止泄漏)这是懒惰的(甚至可能是异步的)产生的.

This can be useful in situations where you want to "tag" objects without actually mutating them (setting a property on them). Lots of algorithms contain some sort of "if x was already seen" condition (a JSON.stringify cycle detection might be a good example), and when you work with user-provided values the use of a Set/WeakSet would be advisable. The advantage of a WeakSet here is that its contents can be garbage-collected while your algorithm is still running, so it helps to reduce memory consumption (or even prevents leaks) when you are dealing with lots of data that is lazily (possibly even asynchronously) produced.

这篇关于ECMAScript 6:WeakSet 有什么用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆