前言
下面只是对于后端所需知识 的补充。并非完整的ES6新特性,如有相应需求可查看官网文档进行学习。
由于JS官方文档是全英文的,不方便大家学习。这里给大家提供一个比较友好的中文文档,请点这里caibaojian
表达式
let和const
1 2
| let a = 3; const b = 5;
|
解构赋值
1 2 3 4 5 6 7 8 9
| const arr = [1,2,3] const[x,y,z] = arr; let person = { name:'zs', age:20 } let {name:username,age} = person
|
字符串相关
字符串扩展
1 2 3 4 5 6 7
| let str = "hello.vue"
str.startsWith("hello")
str.endsWith(".vue")
str.includes("e")
|
字符串模板
1 2 3 4 5 6 7
| let ss = `<div> <span>hello</span> </div> `
let info = '我是${name},今年${age + 10}'
|
函数与对象
函数优化
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| function fun(a,b = 1){ return a + b }
function fun(...values){ return values.length }
var print = (a,b) => console.log(a + b)
var sum = (a,b) => a + b
var normal = (a,b,c) => { let sum = a + b + c return sum }
const person = { name:'jack', age:20 } var hello = ({name}) => console.log("hello" + name)
|
对象优化
灵活利用Object提供的方法,有时候能极大简化代码!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| const person = { name:'jack', age:20 } Object.keys(person) Object.values(person) Object.entries(person)
const one = {a : 1} const two = {b : 2} const three = {c : 3} Object.assign(one,two,three)
const age = 23 const name = 'zs' const person1 = {name: name,age : age}
const person2 = {name,age}
let person3 = { name:'jack', eat:function(food){ console.log(this.name + '吃' + food) } eat2: food => console.log(person.name + '吃' + food) eat3(food){ console.log(this.name + '吃' + food) } }
let person1 = {name:'zs',age:15} let someone = {...person}
let age1 = {age:20} let name1 = {name:'lisi'}
let p1 = {...age1,...name1}
|
数组扩展
map和reduce
map用于处理数组,reduce用于计算数组
1 2 3 4 5 6 7 8 9 10 11 12 13
| let arr = [1,2,5,7,9] arr = arr.map((item) => { item * 2 })
let arr = [10,40,-2,5,-1] arr.reduce((a,b) => { return a + b })
|
Promise异步编排
发起请求
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| let p = new Promise((resolve,reject) => { $.ajax({ url:'www.xxx.com', success:function(data){ console.log("第一次异步获取的结果" + data) resolve(data) } error:function(err){ reject(err) } }) p.then((obj)=>{ return new Promise((resolve,reject) => { $.ajax({ url:` www.other.com + ${obj.id}`, success:function(data){ console.log("第二次异步获取的结果" + data) resolve(data) }, error:function(err){ reject(err) } }) }) }).then((obj2)=>{ }).catch((err)=>{ }) })
|