如果可以使用for...of来迭代可迭代对象,为什么标准不支持在可迭代对象上使用.map(...
?对于包括for...of
的生成器来说,这可能只是一个合成糖,所提供的函数在for循环中运行.
例如,它可以是糖,用于:
function* map(iterable, mapFn) {
for (const item of iterable) {
yield mapFn(item);
}
}
What is the fundamental limitation that prevents this behavior?个
加分:如果后续函数不改变数据,JS引擎(例如,V8)是否会复制数据以将其转换为数组?例如:
const res = [...extremelyLargeSet].reduce((acc,cur)=>acc+cur.value,0)
我通常喜欢Set
和Map
,因为它们是某些应用程序最合适的数据 struct ,但在某些情况下,我担心可能会出现重复.