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>

作者:彼岸舞

时间:2021816

内容关于:前端知识库

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

原文地址:https://www.cnblogs.com/flower-dance/p/15150269.html