今天小编更新的这篇文章相对比较好理解,一个是对对象的扩展,和之前es6中的数组扩展很类似,另外对Promise的扩展,也在query中的ajax类似,内部没有很多,所以小编将这两块内容放在一起更新。期待着大家一起进步。大家还可以关注我的微信公众号,蜗牛全栈。
一、对象拓展
1、es6中的数组拓展运算符
const arr1 = [1,2,3] const arr2 = [4,5,6] const arr3 = [...arr1,...arr2] // [1,2,3,4,5,6]
2、克隆对象
const obj1 = { name:"lilei", age:12 } const obj2 = { work:"teacher" } // 克隆对象:深拷贝 const obj3 = {...obj1} // {name:"lilei",age:12} obj1.age = 18 console.log(obj3) // {name:"lilei",age:12}
3、合并对象:如果属性有相同的,后面的属性值会替换掉前面的属性值
const obj1 = { name:"lilei", age:12 } const obj2 = { work:"teacher", age:20 } const obj4 = {...obj1,...obj2} console.log(obj4) // {name:"lilei",age:20,work:"teacher"}
4、剩余运算符:作为参数的时候,必须放在最后,否则会报
const obj1 = { name:"lilei", age:34, work:"teacher", course:"es" } const {name,age,...rest} = obj1 console.log(name) // lilei console.log(name) // 34 console.log(rest) // { work:"teacher",course:"es"}
二、Promise扩展:finally(异步执行结束之后要执行的内容,必须加载中交互结束和数据库操作之后关闭数据库连接)
new Promise((resolve,reject) => { setTimeout(() => { resolve('success') },1000) }).then(res => { console.log(res) }).catch(err => { console.log(err) }).finally(() => { // 类似ajax里面的complete console.log('finally') }) // success finally