首页
学习
活动
专区
圈层
工具
发布

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

代码语言:javascript
复制
<!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>
下一篇
举报
领券