1月30日,TC39将以下特性加入到了ES-2019\
-
JSON ⊂ ECMAScript (JSON superset)
- 行分隔符(U +2018)和段分隔符(U+2019)允许写在字符串文字中,和JSON匹配\
-
更加友好的JSON.stringify(修复了一些超出范围的unicode展示错误的问题)\
- 以前:
JSON.stringify('\uD800') =>'"?"'- 现在:
JSONstringify('\uD800') => '\\uD800' JSON.stringify('\uD800') === '\\uD800' => true -
Array.sort
- 原来:用的不稳定的quickSort(快排),超过10个元素以上的数据在V8中会不稳定
- 现在:采用了稳定的TimSort算法,解决了这个问题
-
toString
- 原来:funciton.toString()只返回function关键字
- 现在:返回精准字符,包括空格和注释
function /*test func*/ foo() {} foo.toString()\ 原来返回=> 'function foo(){}' 现在返回=> 'function /*test func*/ foo() {}' -
Array新增flat和flatMap方法
- flat:
- 作用:数组降维,返回一个新数组,不改变原来数组
- 接受参数:Number类型,默认为1,根据参数来降低维度层次
const arr = [2,3,[4],[5,[6,[7]]]] arr.flat(1) //=>[2,3,4,5,[6,[7]]] arr.flat(2) // => [2,3,4,5,6,[7]] arr.flat(Infinity) //=>[2,3,4,5,6,7] console.log(arr) // [2,3,[4],[5,[6,[7]]]]- flatMap:
- 作用:对数据进行统一变化处理,返回一个新数组
- 接受参数:func
const arr = [1,2,3] //对每一个元素做处理 arr.glatMap(x => x*2) // [2,4,6] //对一个元素多处理,返回多个对应的值,但是返回必须是数组,里面的类型不限制 arr.flatMap(x => [x,x*2] // [1,2,2,4,3,6] arr.flatMap(x=>[{test:x}] // [{test:1},{test:2},{test;3}] //如果返回{}会全部返回undefined arr.flarMap(x => {x:x}) // [undefined,undefined,undefined] - flat:
-
Object新增fromEntries方法(和Object.entries互补)
- 作用:把Object.entries转换后的二维数组重新转换为对象
- 接受参数:目标数组
const obj = {x:3,y:4}
let entries = Object.entries(obj) // [[x,3],[y,4]]
let obj2 = Object.fromEntries(entries) // {x:3,y:4}
- String新增空格清楚方法trimStart和trimEnd
const str = ' test string '
// trimStart:去除开头空格
str.trimStart() // 'test string '
// trimEnd:去除结尾空格
str.trimEnd() // ' test string'
// trim:首尾空格都去除
str.trim() // 'test string'
-
Symbol新增description属性
- 作用:获取创建Symbol时写的备注
const sym = Symbol('this is remark')
// 原来获取备注方式
String(sym) // 'Symbol(this is remark)'
// 现在
sym.description // 'this is remark'