ECMAScript 6入门扩展笔记

时间:2018-11-02 22:54:25   收藏:0   阅读:164

字符串扩展

Unicode相关补充

引入"\u{xx}"来表示码点,之前只能表示\u0000-\uffff
引入String.fromCodePoint()能正确读取大于uFFFF的unicode编码
引入s.codePiointAt()用于读取大于0xFFFF的字符的unicode编码
例如let s = ‘??a‘读取s的第一个字符

//繁琐,绕,不推荐
s.codePoint(0).toString(16); //20bb7
String.fromCodePoint(0x2bb7)  //??

或者

for (let i of s) {
  console.log(i);
}

或者

Array.from(s)[0]

下面两种都是利用字符串的可迭代;

es6补充的新方法:

includes():返回布尔值,表示是否找到了参数字符串。
startsWith():返回布尔值,表示参数字符串是否在原字符串的头部。
endsWith():返回布尔值,表示参数字符串是否在原字符串的尾部

这个查找应该多用正则匹配较多,而且功能更强大

函数扩展

数组扩展

强大而又常用的数组来了

数组中关于空位的处理

ES5 对空位的处理,已经很不一致了,大多数情况下会忽略空位。
forEach(), filter(), reduce(), every() 和some()都会跳过空位。
map()会跳过空位,但会保留这个值
join()和toString()会将空位视为undefined,而undefined和null会被处理成空字符串
// map方法
[,'a'].map(x => 1) // [,1]
// join方法
[,'a',undefined,null].join('#') // "#a##"
// toString方法
[,'a',undefined,null].toString() // ",a,,"

ES6大部分处理为undefined,例如:Array.from(),(...arr),entries()、keys()、values()、find()和findIndex()
for-of对于空数组遍历会输出undefined

let arr = new Array(3);   
for(let i of arr) {
  console.log(i);    
}                //undefined undefined undefined

应该至少是与迭代器相关的方法都会输出undefined;而fill()copyWithin()方法按空位正常处理,[,‘a‘,‘b‘,,].copyWithin(2,0) // [,"a",,"a"]

原文地址:https://segmentfault.com/a/1190000016888270

原文:https://www.cnblogs.com/lalalagq/p/9898428.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!