在线文档教程

string.@@iterator

string.@@iterator

[@@iterator]() 方法返回一个新的Iterator对象,它遍历字符串的代码点,返回每一个代码点的字符串值。

语法

string[Symbol.iterator]

返回值

一个新的Iterator对象。

示例

使用[@@iterator]()

var string = 'A\uD835\uDC68'; var strIter = string[Symbol.iterator]( console.log(strIter.next().value // "A" console.log(strIter.next().value // "\uD835\uDC68"

通过 for..of 使用[@@iterator]()

var string = 'A\uD835\uDC68B\uD835\uDC69C\uD835\uDC6A'; for (var v of string) { console.log(v } // "A" // "\uD835\uDC68" // "B" // "\uD835\uDC69" // "C" // "\uD835\uDC6A"

规范

SpecificationStatusComment
ECMAScript 2015 (6th Edition, ECMA-262)The definition of 'String.prototype@@iterator' in that specification.StandardInitial definition.
ECMAScript Latest Draft (ECMA-262)The definition of 'String.prototype@@iterator' in that specification.Draft

浏览器兼容性

FeatureChromeEdgeFirefoxInternet ExplorerOperaSafari
Basic Support(Yes)?17 — 27 (as iterator) 27 — 36 (as @@iterator)1 362NoNoNo

FeatureAndroidChrome for AndroidEdge mobileFirefox for AndroidIE mobileOpera AndroidiOS Safari
Basic SupportNoNo?36NoNoNo