问题描述
我想知道在使用 @@ 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上有一个,但在示例本身中,使用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
.
这篇关于ES 6:Symbol.iterator和@@ iterator之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!