let User = {
userName: "猴子们",
userEmail: "10086@163.com",
getInfo: function () {
return `${this.userName} ${this.userEmail}`;
},
};
console.log(User);
//输出:猴子们 10086@163.com
// 进行简化
user = {
userName: userName,
userEmail: userEmail,
getInfo: function ( ){
return `${this.userName} : ${this.userEmail} `;
// 输出 猴子们 10086@163.com
},
};
console.log(userName,userEmail;
// 再次简化
// 对象字面量中的属性值引用的变量,如果与对象在同一个作用域中,则
// 并且这个变量与对象的属性同名,则可以省去不写
// 简化的前提:
// 1.同一个作用域
// 2.变量与属性同名
user = {
userName: " ",
userEmail: " ",
// grtinfo是方法
getInfo: function () {
return `${this.userName} ${this.userEmail}`;
},
};
console.log(user);
//输出:猴子们 10086@163.com
<第三天学习>对象字面量的简化
最新推荐文章于 2024-08-12 09:24:46 发布