本笔记为阅读http://es6.ruanyifeng.com/ 阮老师的文章本身的总结,仅做自用,感谢阮老师的技术分享。es6
1.超过uFFFF的字符ES5没法正确显示,会显示成前面的uFFFF的码点符号加四位后面的码点符号,如u20BB7会解析为u20BB+7,ES6中将码点写在大括号中便可正确输出。
ES6共有6中方法表示一个字符:this
'\z' === 'z' // true '\172' === 'z' // true '\x7A' === 'z' // true '\u007A' === 'z' // true '\u{7A}' === 'z' // true
2.js内部存储字符默认是两个字节,对于4个字节的js会将其视为两个字符。
ES5提供charCodeAt方法(s.charCodeAt),ES6新增codePointAt方法。code
3.ES5提供fromCharCode,ES6提供fromCodePoint方法,从码点返回字符。接口
4.字符串的遍历器接口:ip
for(let i of 'foo'){ console.log(i) }//'f''o''o'
5.ES6新增判断字符串的包含状况(ES5->indexOf):字符串
includes: 返回bool值,表示是否找到了字符串。 startsWith: 返回bool值, 表示参数字符串是否在原字符串的头部。 endsWith: 返回bool值,表示参数字符串是否在原字符串的尾部。
这三个方法都支持第二个参数,表示开始搜索的位置:get
let s = 'hello world' s.includes('hello',5)//false s.startsWith('world',6)//true s.endsWith('hello',5)//true(endsWith搜索前n个字符)
6.repeat返回一个字符串吗,表示将原字符串重复n次,string
're'.repeat(3)//'rerere'
7.padStart,padEnd用于补全字符串,前者从头部开始补全,后者从尾部补全。it
'x'.padStart(5,'as')//'asasx' 'b'.padEnd(5,'fg')//'bfgfg' 'j'.padStart(5)//' j'(默认使用空格补全)
8.模板字符串。反引号``括起来console
// 普通字符串 `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 = 'asd', time = 'today' `${time} ,my name is ${name}`
后面的模板编译看不懂