ES6-声明对象和方法

<html>

<head>
    <title>ES6-声明对象和方法</title>
</head>

<body>




    <script>
        //定义变量,
        {
            //下面效果是一样的 ,看起来  es6更加简洁
            const age = 12;
            const name = "Amy";
            // 传统
            const person1 = { age: age, name: name };
            console.log(person1);//{age: 12, name: "Amy"};
            // ES
            const person2 = { age, name };
            console.log(person2) //{age: 12, name: "Amy"};
        }
        //定义方法
        {
            // 传统
            const person1 = {
                sayHi: function () {
                    console.log("Hi")
                }
            }
            person1.sayHi();//"Hi"
            // ES6
            const person2 = {
                sayHi() {
                    console.log("Hi");
                }

            }
            person2.sayHi();  //"Hi"

        
        }  </script>
</body>

</html>

 

posted @ 2020-09-26 22:00  KwFruit  阅读(157)  评论(0编辑  收藏  举报