ECMAScript 6 Map & Set - 在迭代过程中可以安全删除吗?
ECMAScript 6 Map & Set - Safe to delete during iteration?
如标题所示,迭代新的 Set 和 Map 类型并在迭代期间删除条目是否安全?
例如,在 Java 中,这会导致 ConcurrentModificationExceptions。
我在规范中找不到关于此问题的任何信息。所以我想这是允许的??
引用 MapIterator object's next()
section,
Let entries be the List that is the value of the [[MapData]] internal slot of m.
Repeat while index is less than the total number of elements of entries. The number of elements must be redetermined each time this method is evaluated.
...
...
- Set the [[Map]] internal slot of O to undefined.
- Return CreateIterResultObject(undefined, true).
与SetIterator object's next()
section、
类似
- Let entries be the List that is the value of the [[SetData]] internal slot of s.
- Repeat while index is less than the total number of elements of entries. The number of elements must be redetermined each time this method is evaluated.
...
...
- Set the [[IteratedSet]] internal slot of O to undefined.
- Return CreateIterResultObject(undefined, true).
由于每次调用 next()
时都会重新计算值,如果当前指针 (index) 大于条目的长度,则它不会' 抛出错误,而是 returns undefined
.
但是,一般来说,避免在迭代容器时修改容器。
如标题所示,迭代新的 Set 和 Map 类型并在迭代期间删除条目是否安全?
例如,在 Java 中,这会导致 ConcurrentModificationExceptions。
我在规范中找不到关于此问题的任何信息。所以我想这是允许的??
引用 MapIterator object's next()
section,
Let entries be the List that is the value of the [[MapData]] internal slot of m.
Repeat while index is less than the total number of elements of entries. The number of elements must be redetermined each time this method is evaluated.
... ...
- Set the [[Map]] internal slot of O to undefined.
- Return CreateIterResultObject(undefined, true).
与SetIterator object's next()
section、
- Let entries be the List that is the value of the [[SetData]] internal slot of s.
- Repeat while index is less than the total number of elements of entries. The number of elements must be redetermined each time this method is evaluated.
... ...
- Set the [[IteratedSet]] internal slot of O to undefined.
- Return CreateIterResultObject(undefined, true).
由于每次调用 next()
时都会重新计算值,如果当前指针 (index) 大于条目的长度,则它不会' 抛出错误,而是 returns undefined
.
但是,一般来说,避免在迭代容器时修改容器。