//原始组合对象的写法
<script type="text/javascript">
const name = 'Asher';
const age = 3;
const birth = '2015-09';
const user = {
name: name,
age: age,
birth: birth
}
console.log(user); //{name: "Asher", age: 3, birth: "2015-09"}
</script>
//属性名简写:当属性名和表示值的变量名一样时,只写一个就可以了
<script type="text/javascript">
const name = 'Asher';
const age = 3;
const birth = '2015-09';
const user = {
name,
age,
birth
}
console.log(user); //{name: "Asher", age: 3, birth: "2015-09"}
</script>
//对象字面量方法的简写
<script type="text/javascript">
const name = 'Asher';
const age = 3;
const birth = '2015-09';
const user = {
name,
age,
birth,
getName() {
console.log(this.name);
}
}
user.getName(); //Asher
</script>
//对象字面量的计算属性
//组合对象时不用一个个添加属性
<script type="text/javascript">
const keys = ['name','age','birth'];
const values = ['Asher',3,'2015-09'];
const user = {
[keys.shift()]: values.shift(),
[keys.shift()]: values.shift(),
[keys.shift()]: values.shift()
}
console.log(user); //{name: "Asher", age: 3, birth: "2015-09"}
</script>
网友评论