1、对象解构是为了简化对象属性取值

解构没有顺序,但要和对象属性名称保持一致,如下:

  1. <script>
  2. const user = {
  3. username:'张三',
  4. age:28,
  5. getUsername(){
  6. return this.username;
  7. }
  8. }
  9. console.log(user.username);
  10. console.log(user.age);
  11. console.log(user.getUsername());
  12. let {username,age} = user;
  13. console.log(age);
  14. </script>

2、解构也可以定义到对象的函数方法中

  1. <script>
  2. let { abs,random } = Math;
  3. console.log(abs(-1));
  4. console.log(random());
  5. </script>