es6常用语法
2021-12-27 本文已影响0人
银角大王__
ES6常用语法
1. let const
/* let 和 const
需要改变的变量用let
*/
let foo = 'aa'
const cc = '234'
/* 2.模板字符串 */
let str = `aaa${foo}`
/* 3.箭头函数 */
setTimeout(() => {
}, timeout);
/* 4. 结构赋值 */
const obj = {age:13,name:'xxx'}
let { age } = obj
age // 13
/* 5. 扩展运算符 ... */
// 合并数组
const arr1 = ['a', 'b'];
const arr2 = ['c'];
const arr3 = ['d', 'e'];
// ES6 的合并数组
[...arr1, ...arr2, ...arr3]
// [...'hello']
// [ "h", "e", "l", "l", "o" ]
/* 6. Promise */
const promise = new Promise(function (resolve, reject) {
if (true) {
resolve('value')
} else {
reject('error')
}
})
promise.then(res => {
//成功
}).catch(err => {
//失败
})
const p1 = new Promise(function(resolve,reject){
})
const p2 = new Promise(function(resolve,reject){
})
const alls = Promise.all([p1,p2]).then(res=>{
[res]
})
/* 7. async await*/
async function asyncPrint(value, ms) {
await timeout(ms);
console.log(value);
}