JS新特性

131 阅读1分钟

ES6

1. 模块化

//模块A导出一个方法
export const sub = (a, b) => a + b

//模块b导入使用
import { sub } from './A'
console.log(sub(1,2)) // 3

2. 箭头函数

const func = (a,b) => a + b
func(1,2) // 3

3. 函数参数默认值

function foo (age = 25) { //...}

4. 解构赋值

let a = 1, b = 2
[a, b] = [b, a] // a 2  b 1

5. 延展操作符

6. 模版字符串

const name = 'zf'
cosnt str = `my name is ${name}`

8. 对象属性简写

const name = 'zf'
const obj = { name }

9. promise

Promise.resolve().then(()=>{console.log(2)})
console.log(1)

先打印1, 再打印2

ES7

1. Array.prototype.includes()

[1].includes(1)   // true

2. 指数操作符

2**10  // 1024

ES8

1. async/await 异步终极解决方案

async getData(){
    const res = await getTableData() // await 异步操作
    //do something
}

2. Object.values(), Object.keys(), Object.entries()

Object.values({a:1,b:2,c:3}) // [1, 2, 3]
Object.keys({a:1,b:2,c:3})  // [a, b, c]
OBject.entries({a:1,b:2,c:3})  // [['a', 1],['b', 2],['c', 3]]

3. String padding

'zf'.padStart(5,'*')  // ***zf
'20',padEnd(5,'.00)  // 20.00

ES9

1. 异步迭代

  • await可以和for...of循环一起使用, 以串行的方式运行异步操作
async function process(array){
    for await (let i 0f array){
        // do something
    }
}

2. rest/spread属性

const values = [1,2,3,4]
console.log(Math.max(...values)) // 4

3. 正则表达式命名捕获组

const reg = /(?<year>[0-9]{4})-(?<month>[0-9]{2})-(?<day>[0-9]{2})/
const test = reg.exec('2020-01-29')

截屏2021-06-11 上午11.00.05.png 截屏2021-06-11 上午11.00.16.png