ESx 新特性

228 阅读1分钟

1.arr.flat() 

 (1)递归地将嵌套数组拼合到制定深度

    const arr = [1, 2, [3, 4]]

    arr.flat()   // [1,2,3,4]

    const arr = [1, 2, [3, 4, [5, 6, [7, 8]]]]

   arr.flat(Infinity)   // [1,2,3,4,5,6,7,8]

 (2)该方法移除数组中的空项

   const arr = [1,2,,4,5]

  arr.flat()     // [1,2,4,5]

2.arr.flatMap()

(1)使用映射函数映射每个元素,然后把结果压缩成一个新数组

const arr = [1,2,3]

arr.flatMap(x => [x * 4])    // [4,8,12]

3.string.trimStart()   string.trimEnd()   

trimStart(): 移除原字符串左端的连续空白符并返回,并不会直接修改原字符串本身

trimEnd() : 移除原字符串右端的连续空白符并返回,并不会直接修改原字符串本身

const test = ' hello '

test.trim()     // 'hello'

test.trimStart()     // 'hello '

test.trimEnd()     // ' hello'

4.object.fromEntries  将键值对转换为object 的新方法

const obj = {prop1: 2,prop2: 10, prop3: 15}

let arr = Object.entries(obj)    // [['props1', 2], ['props2', 10], ['props', 15]]

const newObj = Object.fromEntries(arr)

// {prop1: 2, prop2: 10, prop3: 15}

5.Symbol.description

description : readonly, 返回Symbol 对象的可选描述的自渡船,用来代替toString() 方法

const test = Symbol('desc')

test.description    // 'desc'

test.toString()      // 'Symbol(desc)'

6. fun.toString() 在函数上调用toString() 会返回该函数