ES 6: Difference between Symbol.iterator and @@iterator

后端 未结 2 1640
傲寒
傲寒 2021-01-04 04:54

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

<
2条回答
  •  囚心锁ツ
    2021-01-04 05:24

    The ECMAScript 2015 (ES6) specification uses @@iterator to reference Symbol.iterator. There is no @@iterator, and wherever you see it, read it as Symbol.iterator.

    I think (someone more familiar with the ECMAScript spec might be able to back this up) the reason @@iterator is used is because you need to access Symbol properties on an object using square brackets ([]), but most parts of the ES2015 specification (and documentation) used the more familiar dot-notion (e.g. object.property versus object["property"]), and so@@somesymbol` is used to keep things consistent.

    See page 38 (marked as page 18): http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf

提交回复
热议问题