【问题标题】:Why getter returns old value为什么getter返回旧值
【发布时间】:2023-03-27 10:05:01
【问题描述】:

我有以下代码:

function User(fullName) {
    this.fullName = fullName;
    Object.defineProperties(this,
        {
            firstName: {
                get: function () {
                    return fullName.split(" ")[0];
                }
                ,
                set: function (fName) {
                    this.fullName = fName + " " + this.lastName;
                }
            },
            lastName: {
                get: function () {
                    return fullName.split(" ")[1];
                }
                ,
                set: function (lName) {
                    this.fullName = this.firstName + " " + lName;
                }
            }
        })

}

以及以下要执行的代码:

var vasya = new User("oldName oldSurname");

console.log(vasya.firstName); // 


vasya.firstName = "newName";
vasya.lastName = "newSurname"

console.log(vasya.fullName);

这会输出newName OldSurname

如果稍微改变一下:

var vasya = new User("oldName oldSurname");

console.log(vasya.firstName); //
console.log(vasya.lastName); //

vasya.firstName = "newName";
vasya.lastName = "newSurname"

console.log(vasya.fullName);

它返回oldName newSurname

请解释为什么我现在看到oldName insted of newName

【问题讨论】:

    标签: javascript get set


    【解决方案1】:

    我玩过这段代码,发现它是命名冲突。 此变体正常工作

    function User(fullNameValue) {
        this.fullName = fullNameValue; // renamed function argument
        Object.defineProperties(this,
            {
                firstName: {
                    get: function () {
                        return this.fullName.split(" ")[0];//I use this here. without it I returned function argument
                    }
                    ,
                    set: function (fName) {
                        this.fullName = fName + " " + this.lastName; 
                    }
                },
                lastName: {
                    get: function () {
                        return this.fullName.split(" ")[1];//I use this here. without it I 
                    }
                    ,
                    set: function (lName) {
                        this.fullName = this.firstName + " " + lName;
                    }
                }
            })
    
    }
    

    【讨论】:

      【解决方案2】:

      引用 fullNameValue 时必须使用“this”关键字,否则它将使用您作为参数传递的 var

      function User(fullName) {
          this.fullName = fullName;
          Object.defineProperties(this,
          {
              firstName: {
                  get: function () {
                      return this.fullName.split(" ")[0];
                  }
                  ,
                  set: function (fName) {
                      this.fullName = fName + " " + this.lastName;
                  }
              },
              lastName: {
                  get: function () {
                      return this.fullName.split(" ")[1];
                  }
                  ,
                  set: function (lName) {
                      this.fullName = this.firstName + " " + lName;
                  }
              }
          })
      
      }
      

      【讨论】:

        猜你喜欢
        • 2020-05-07
        • 1970-01-01
        • 2021-08-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多