当前位置:网站首页>5 基本引用类型
5 基本引用类型
2022-08-04 15:38:00 【你回到了你的家】
3 原始值包装类型
3.3 String
3.3.9 字符迭代与解构
待补充 @@iterator是什么
字符串的原型上暴露了一个@@iterator 方法,表示可以迭代字符串的每个字符。可以像下面这样手动使用迭代器:
let message = "abc";
let stringIterator = message[Symbol.iterator]();
console.log(stringIterator.next()); // {value: "a", done: false}
console.log(stringIterator.next()); // {value: "b", done: false}
console.log(stringIterator.next()); // {value: "c", done: false}
console.log(stringIterator.next()); // {value: undefined, done: true}
在 for-of 循环中可以通过这个迭代器按序访问每个字符:
for (const c of "abcde") {
console.log(c);
}
// a
// b
// c
// d
// e
有了这个迭代器之后,字符串就可以通过解构操作符来解构了。比如,可以更方便地把字符串分割为字符数组:
let message = "abcde";
console.log([...message]); // ["a", "b", "c", "d", "e"]
边栏推荐
猜你喜欢
随机推荐
Taurus.MVC WebAPI 入门开发教程2:添加控制器输出Hello World。
多线程编程之优先级翻转问题
现代 ABAP 编程语言中的正则表达式
【北亚数据恢复】IBM System Storage存储lvm信息丢失,卷访问不了的数据恢复方案
365天挑战LeetCode1000题——Day 049 非递增顺序的最小子序列 贪心
In action: 10 ways to implement delayed tasks, with code!
postman “header“:{“retCode“:“999999“
inter-process communication
remote: Check Access Error, please check your access right or username and password!fatal: Authenti
Tinymce plugins [Tinymce 扩展插件集合]
How to monitor code cyclomatic complexity by refactoring indicators
qt 复杂界面信号槽设计
字节API鉴权方法
如何防止重复下单?
你以为在做的是微服务?不!你做的只是分布式单体!
RepVGG学习笔记
全球电子产品需求放缓,三星手机越南工厂每周只需要干 3~4 天
2022年7月国产数据库大事记-墨天轮
从-99打造Sentinel高可用集群限流中间件
UWP 转换 IBuffer 和其他类型







