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"
规范
Specification | Status | Comment |
---|---|---|
ECMAScript 2015 (6th Edition, ECMA-262)The definition of 'String.prototype@@iterator' in that specification. | Standard | Initial definition. |
ECMAScript Latest Draft (ECMA-262)The definition of 'String.prototype@@iterator' in that specification. | Draft | |
浏览器兼容性
Feature | Chrome | Edge | Firefox | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic Support | (Yes) | ? | 17 — 27 (as iterator) 27 — 36 (as @@iterator)1 362 | No | No | No |
Feature | Android | Chrome for Android | Edge mobile | Firefox for Android | IE mobile | Opera Android | iOS Safari |
---|---|---|---|---|---|---|---|
Basic Support | No | No | ? | 36 | No | No | No |