当前位置:网站首页>【ES6】字符串的扩展
【ES6】字符串的扩展
2022-04-21 21:56:00 【小沈曰】
1. 字符的Unicode表示法
ES6加强了对Unicode的支持,允许采用\uxxxx形式表示一个字符,其中xxxx表示字符的Unicode码点
'\u0061'
// 'a'
2. 字符串的遍历器接口
ES6为字符串添加了遍历器接口,使得字符串可以被for...of循环遍历。
for(let codePoint of 'foo'){
console.log(codePoint)
}
// 'f'
// 'o'
// 'o'
3. JSON.stringify() 的改造
根据标准,JSON 数据必须是 UTF-8 编码。但是现在的JSON.stringify()方法有可能返回不符合 UTF-8 标准的字符串。
具体来说,UTF-8 标准规定,0xD800到0xDFFF之间的码点,不能单独使用,必须配对使用。比如,\uD834\uDF06是两个码点,但是必须放在一起配对使用,代表字符𝌆。这是为了表示码点大于0xFFFF的字符的一种变通方法。单独使用\uD834和\uDFO6这两个码点是不合法的,或者颠倒顺序也不行,因为\uDF06\uD834并没有对应的字符。
JSON.stringify()的问题在于,它可能返回0xD800到0xDFFF之间的单个码点。
JSON.stringify('\u{D834}') // "\u{D834}"
为了确保返回的是合法的 UTF-8 字符,ES2019 改变了JSON.stringify()的行为。如果遇到0xD800到0xDFFF之间的单个码点,或者不存在的配对形式,它会返回转义字符串,留给应用自己决定下一步的处理。
JSON.stringify('\u{D834}') // ""\\uD834""
JSON.stringify('\uDF06\uD834') // ""\\udf06\\ud834""
4. 模板字符串
传统的JavaScript语言,输出模板通常是以+拼接
$('#result').append(
'There are <b>' + basket.count + '</b> ' +
'items in your basket, ' +
'<em>' + basket.onSale +
'</em> are on sale!'
);
上面这种写法相当繁琐不方便,ES6 引入了模板字符串解决这个问题。
$('#result').append(` There are <b>${
basket.count}</b> items in your basket, <em>${
basket.onSale}</em> are on sale! `);
模板字符串(template string)是增强版的字符串,用反引号(`)标识。它可以当作普通字符串使用,也可以用来定义多行字符串,或者在字符串中嵌入变量。
// 普通字符串
`In JavaScript '\n' is a line-feed.`
// 多行字符串
`In JavaScript this is not legal.`
console.log(`string text line 1 string text line 2`);
// 字符串中嵌入变量
let name = "Bob", time = "today";
`Hello ${
name}, how are you ${
time}?`
模板字符串中还能调用函数
function fn(){
return 'hello world'
}
`foo ${
fn()} bar`
//foo hello world
版权声明
本文为[小沈曰]所创,转载请带上原文链接,感谢
https://szlsay.blog.csdn.net/article/details/124188455
边栏推荐
- Ffmpeg serial 2 - separate video and audio
- WPF data-driven method for modifying binding
- 怎么用vbs实现微信自动发送消息功能
- 省选游记暨后期基础规划
- Automatically generate SQL excluding a field
- MySQL多表查询小练习
- Analysts believe that Samsung Galaxy Z fold 4 and Z flip 4 may be cheaper than their previous products
- 数据库事务学习总结
- Crisis is everywhere. Can the high-end human equipment of CASS yogurt still stand?
- Detailed explanation of redis configuration file
猜你喜欢

【JVM】10道不得不会的JVM面试题

软件的生命周期
![[test case level definition]](/img/e8/a2c7dee5f76b6094ff8c39f33307fe.png)
[test case level definition]

FFmpeg连载1-环境搭建

Architecture document of outsourcing student management system

ROS机器人从起点到终点(四)蓝桥云实践复现

what? Your company has not set the JVM initial and maximum heap memory size to the same value?
![[charming Java] - data types and variables](/img/d1/22122803b7b796fbc5e269219d6db8.png)
[charming Java] - data types and variables

Live555 learning

分析师认为三星Galaxy Z Fold 4和Z Flip 4可能比其前代产品更便宜
随机推荐
在线YAML转Properties工具
What should you do to benefit from digital transformation?
硬核实力,多方认可|云扩科技作为RPA核心厂商入选《2022中国RPA采购指南》
[UML operation contract]
MYPINPAD和SmartPesa合并,成为移动支付受理领域的全球领导者
Practice of JVM custom class loader in code extensibility
怎么用vbs实现微信自动发送消息功能
WPF data-driven method for modifying binding
FFmpeg连载1-环境搭建
数据库事务学习总结
[summary of the most complete bat must ask high concurrency in history]
Mongo geonear query Chinese fuzzy search in PHP
2022r1 quick opening pressure vessel operation certificate examination question bank simulation examination platform operation
RFC 之间的关系
Oracle合并数据操作(MERGE)
Serviceworker cache and HTTP cache
软件测试分类与软件测试的原则
Finding a way to combine new technologies with itself is the key to the development of industrial Internet
Alibaba cloud OSS, user authentication and patient
Makefile file configure executable file and cflags parameter