ES6参考---简化的对象写法

ES6参考---简化的对象写法

一、总结

一句话总结:

主要是把键值对写法变成只有一部分,一部分是同名属性赋值,另一部分是函数赋值

1、省略同名的属性值:x : x 写成 x,
2、省略方法的function:getPoint : function () {} 写成 getPoint(){}

1、简化的对象写法的两种情况?

1、省略同名的属性值:x : x 写成 x,
2、省略方法的function:getPoint : function () {} 写成 getPoint(){}
let x = 3;
let y = 5;
//普通额写法
// let obj = {
//    x : x,
//    y : y,
//    getPoint : function () {
//        return this.x + this.y
//    }
// };
//简化的写法
let obj = {
    x,
    y,
    getPoint(){
        return this.x
    }
};
console.log(obj, obj.getPoint());

二、简化的对象写法

博客对应课程的视频位置:

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4   <meta charset="UTF-8">
 5   <title>05_简化的对象写法</title>
 6 </head>
 7 <body>
 8 <!--
 9 简化的对象写法
10 * 省略同名的属性值
11 * 省略方法的function
12 * 例如:
13   let x = 1;
14   let y = 2;
15   let point = {
16     x,
17     y,
18     setX (x) {this.x = x}
19   };
20 -->
21 <script type="text/javascript">
22 
23     let x = 3;
24     let y = 5;
25     //普通额写法
26 //    let obj = {
27 //        x : x,
28 //        y : y,
29 //        getPoint : function () {
30 //            return this.x + this.y
31 //        }
32 //    };
33     //简化的写法
34     let obj = {
35         x,
36         y,
37         getPoint(){
38             return this.x
39         }
40     };
41     console.log(obj, obj.getPoint());
42 </script>
43 </body>
44 </html>
 
原文地址:https://www.cnblogs.com/Renyi-Fan/p/12556208.html