1、es6中允许在大括号里直接写属性和方法来简化语法

  1. <script>
  2. function add(){
  3. };
  4. let add1 = () => {
  5. }
  6. let name = "azhi";
  7. let user = {
  8. //属性名和属性值是同样的可以省略
  9. name,
  10. age: 18,
  11. getUsername: function () {
  12. console.log("es5写法");
  13. },
  14. getUsername1() {
  15. console.log("es6写法,在大括号里的函数可以简化省略: function");
  16. },
  17. }
  18. console.log(user.name);
  19. </script>

2、属性名表达式

  1. <script>
  2. let name = "azhi";
  3. let user = {
  4. [name] : "lz",
  5. age : 18,
  6. }
  7. console.log(user.name); //undefined
  8. console.log(user.azhi); //lz
  9. </script>

3、对象扩展运算符

  1. <script>
  2. let user = {
  3. name: "azhi",
  4. age: 18,
  5. }
  6. let user1 = { ...user, sex: "男" }
  7. let user2 = {
  8. ...user1, getName: function () {
  9. console.log(this.name);
  10. }
  11. }
  12. let user3 = {
  13. ...user2, getAge() {
  14. console.log(this.age);
  15. }
  16. }
  17. console.log(user1.sex);
  18. user2.getName();
  19. user3.getAge();
  20. </script>