0
点赞
收藏
分享

微信扫一扫

<第三天学习>对象字面量的简化

晴儿成长记 2022-04-01 阅读 27
javascript
    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

举报

相关推荐

0 条评论