ES6 语法详解(简化的对象写法)

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
    </head>
    <body>
    </body>
    <script>
        /**
         * 简化的对象写法
         * 省略同名的属性值
         * 省略方法的function
         */
        let username = 'dance'
        let age = 18

        // 省略同名的属性值
        let obj = {
            username,
            age,
            getUserName() { // 省略方法的function
                return this.username
            },
            getAge: function() { // 不省略方法的function
                return this.age
            }
        }
        console.log(obj)
        console.log(obj.getAge())
        console.log(obj.getUserName())
    </script>
</html>

作者:彼岸舞

时间:2021\08\16

内容关于:前端知识库

本文属于作者原创,未经允许,禁止转发

posted @ 2021-08-16 22:24  彼岸舞  阅读(59)  评论(0编辑  收藏  举报