1、es6中允许在大括号里直接写属性和方法来简化语法
<script>
function add(){
};
let add1 = () => {
}
let name = "azhi";
let user = {
//属性名和属性值是同样的可以省略
name,
age: 18,
getUsername: function () {
console.log("es5写法");
},
getUsername1() {
console.log("es6写法,在大括号里的函数可以简化省略: function");
},
}
console.log(user.name);
</script>
2、属性名表达式
<script>
let name = "azhi";
let user = {
[name] : "lz",
age : 18,
}
console.log(user.name); //undefined
console.log(user.azhi); //lz
</script>
3、对象扩展运算符
<script>
let user = {
name: "azhi",
age: 18,
}
let user1 = { ...user, sex: "男" }
let user2 = {
...user1, getName: function () {
console.log(this.name);
}
}
let user3 = {
...user2, getAge() {
console.log(this.age);
}
}
console.log(user1.sex);
user2.getName();
user3.getAge();
</script>