ES 6:Symbol.iterator和@@ iterator之间的区别

ES 6:Symbol.iterator和@@ iterator之间的区别

问题描述:

我想知道在使用 @@ iterator 函数与 Symbol.iterator one:

I was wondering if there was a specific difference in implementing an iterator using the @@iterator function versus the Symbol.iterator one:

在MDN上有一个 Array.prototype。@@ iterator ,但在示例本身中,使用Symbol.iterator作为函数名。这只是更新的版本,而 @@ iterator 不再有效,还是两者都有效?

On MDN, there is a page on Array.prototype.@@iterator, yet in the examples itself, Symbol.iterator is used as the function name. Is this just the updated version and @@iterator is not valid anymore, or are both of them valid?

没有 @@ iterator 函数。该标记产生语法错误 - 仅在规范中使用表示特定符号。

There is no @@iterator function. That token produces a syntax error - it is only used in specification to denote a specific symbol.

如果要在代码中使用该符号,例如要访问数组上的迭代器,您必须使用 Symbol.iterator 。这是 Symbol 类的属性,初始化为 @@ iterator

If you want to use that symbol in your code, e.g. to access iterators on arrays, you have to use Symbol.iterator. Which is a property of the Symbol class initialised to have the value @@iterator.