vue-对象字面量的增强写法

1、分别进行对象字面量的几种展示方式:

<body>
  <script>
    //平常对象字面量的写法
    // const obj={
    //   name:'kobro',
    //   age:18,
    //   height:1.88,
    //   run:function(){
    //     console.log('在奔跑!');
    //   },
    //   eat:function(){
    //     console.log('在吃饭!');
    //   }

    // }
    //1、属性的增强写法
    const name='why';
    const age=18;
    const height=1.88
    //通过先定义变量的属性,在一次进行放入,然后进行打印出来
    const obj={
      name:name,
      age:age,
      height:height
    }
    console.log(obj);//因此进行方UR,然后打印出来
  </script>

运行结果:

 

此时已然将定义的属性进行了依次放入!

2、es6的属性字面量的写法:

 console.log(obj);//因此进行方UR,然后打印出来
    // ES6的写法
    const obj1={
      name,
      age,
      height
    }
    console.log(obj);

 

 

 

此时均可以实现!

3、es6和es5的函数增强的写法:

 //es5的函数写法
    const obj2={
      run:function(){

      },
      eat:function(){

      }
    }
    //es6的函数增强写法
    const obj3={
      run(){

      },
      eat(){
        
      }
    }

 

posted @ 2022-03-04 23:08  starter123  阅读(114)  评论(0)    收藏  举报