Javascript 如何在 ES6 ( EcmaScript 2015 ) 中获取 Set 的第一个元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32539354/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the first element of Set in ES6 ( EcmaScript 2015)
提问by JavaScripter
In ES6, how do we quickly get the element?
在 ES6 中,我们如何快速获取元素?
in MDN Syntax for Set, I didn't find an answer for it.
在MDN Syntax for Set 中,我没有找到答案。
回答by MinusFour
They don't seem to expose the List to be accesible from the instanced Object. This is from the EcmaScript Draft:
他们似乎没有将 List 公开为可从实例化对象访问。这是来自 EcmaScript 草案:
23.2.4 Properties of Set Instances
Set instances are ordinary objects that inherit properties from the Set prototype. Set instances also have a [[SetData]] internal slot.
23.2.4 集合实例的属性
Set 实例是从 Set 原型继承属性的普通对象。Set 实例也有一个 [[SetData]] 内部槽。
[[SetData]] is the list of Values the Set is holding.
[[SetData]] 是 Set 持有的值列表。
A possible solution (an a somewhat expensive one) is to grab an iterator and then call next()
for the first value:
一个可能的解决方案(一个有点昂贵的解决方案)是获取一个迭代器,然后调用next()
第一个值:
var x = new Set();
x.add(1);
x.add({ a: 2 });
//get iterator:
var it = x.values();
//get first entry:
var first = it.next();
//get value out of the iterator entry:
var value = first.value;
console.log(value); //1
Worth mention too that:
也值得一提的是:
Set.prototype.values === Set.prototype.keys