【ECMAScript 5_6_7】5、ES6——簡化的物件寫法
阿新 • • 發佈:2019-01-09
一、簡化的物件寫法
簡化的物件寫法 * 省略同名的屬性值 * 省略方法的function * 例如: let x = 1; let y = 2; let point = { x, y, setX (x) {this.x = x} };
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>05_簡化的物件寫法</title> </head> <body> <script type="text/javascript"> let username = 'onedean' let age = 20 let obj = { username:username, age:age, getName:function () { return this.username } } console.log(obj) console.log(obj.getName()) let obj1 = { username, // 省略同名的屬性值 age, getName(){ // 省略方法的function return this.username } } console.log(obj1) console.log(obj1.username) </script> </body> </html>