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

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

问题描述

WeakSet应该通过弱引用来存储元素。也就是说,如果一个对象没有被引用,应该从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中删除。控制台打印:

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

为什么是吗?



另外,还有一个问题。直接向WeakSets添加对象的要点如下:

  weakset.add({name:Charlie}) ; 

这些Traceur的故障或我是否缺少某些东西?



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

解决方案

blockquote>

它没有从WeakSet中删除。为什么会这样?


很可能是因为垃圾收集器尚未运行。但是,您说您正在使用Traceur,所以它可能是没有得到正确的支持。我不知道控制台如何可以显示 WeakSet 的内容。


直接向WeakSets添加对象的要点是什么?


绝对没有将对象文字添加到 WeakSet 的点。


什么是实际使用WeakSet如果我们甚至不能遍历它,也不能得到当前的大小?


你可以得到的是一点信息:对象(或一般地,价值)包含在集合中



这在您想要标记对象而不实际突变它们的情况下(在其上设置属性) )。许多算法包含某些if $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $一个很好的例子),当你使用用户提供的值时,使用 Set / 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.

I have written the following test:

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

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

console.log(weakset);

numbers = undefined;

console.log(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"}}

Why is that?

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

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

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

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

解决方案

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

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.

What is the point of adding objects to WeakSets directly?

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

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?

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天全站免登陆