arr.push(…[4,5])

数组合并

  1. //es5
  2. let book1 = ['平凡的世界第一部', '平凡的世界第二部', '平凡的世界第三部']
  3. let book2 = ['人生']
  4. let book3 = book1.concat(book2);
  5. //console.log(book3) // (4) ["平凡的世界第一部", "平凡的世界第二部", "平凡的世界第三部", "人生"]
  6. //es6
  7. let book4 = [...book1, ...book2];
  8. book4[3] = '月夜静悄悄'
  9. console.log(book4) //(4) ["平凡的世界第一部", "平凡的世界第二部", "平凡的世界第三部", "月夜静悄悄"]

对象合并

  1. let obj1 = {
  2. book1: '平凡的世界第一部',
  3. book2: '平凡的世界第二部',
  4. book3:'平凡的世界第三部',
  5. }
  6. let obj2 = {
  7. ...obj1,
  8. book4:'人生',
  9. }
  10. console.log(obj2) //{book1: "平凡的世界第一部", book2: "平凡的世界第二部", book3: "平凡的世界第三部", book4: "人生"}
  11. //如后者与前者相同,后者覆盖前者
  12. let obj1 = {
  13. book1: '平凡的世界第一部',
  14. book2: '平凡的世界第二部',
  15. book3: '平凡的世界第三部',
  16. book4: '月夜静悄悄',
  17. }
  18. let obj2 = {
  19. ...obj1,
  20. book4: '月夜静悄悄11111',
  21. book5: '人生',
  22. }
  23. console.log(obj2) //{book1: "平凡的世界第一部", book2: "平凡的世界第二部", book3: "平凡的世界第三部", book4: "月夜静悄悄11111", book5: "人生"}
  24. let obj = { name: 'daisy', }
  25. let obj1 = { job: 'web', }
  26. let obj2 = { sex: 1, }
  27. let obj5 = { ...obj, ...obj1, ...obj2 }
  28. console.log(obj5) //{name: "daisy", job: "web", sex: 1}
  29. //与Object.assign用法相同 Object.assign(target, ...sources)
  30. let obj4 = Object.assign(obj, obj1, obj2);
  31. console.log(obj4) //{name: "daisy", job: "web", sex: 1}
  32. console.log(obj) //{name: "daisy", job: "web", sex: 1} /**注意目标对象也会随之改变 */

键和值同名可以简写

  1. var name="lisi";
  2. var age=19;
  3. var obj={
  4. name,age}

解构(主要针对json对象)

  1. var obj=[
  2. {name:"lisi",age:18,likes:["lol","足球"],sex:"male"},
  3. {name:"xixi",age:17,likes:["可乐","白的"],sex:"female"},
  4. {name:"baibai",age:21,likes:["英语","足球"],sex:"male"}
  5. ]
  6. var {name}=obj

模板字符串(``)

  1. `in javascript '\n' is a line-feed.`
  2. var name="Bob",time="today";
  3. `hello ${name},how are you ${time}?`