【问题标题】:"undefined" in get of Object.defineProperty in Constructor在构造函数中获取 Object.defineProperty 中的“未定义”
【发布时间】:2017-12-22 11:00:17
【问题描述】:

我无法理解我在显示 firstName 和 lastName 方法的当前值的方式上做错了什么。现在我在 jone.Name 和 jone.last 中有一个错误,因为它们得到 - 未定义。

function User(fullName) {
  this.fullName = fullName.split(' ');

  Object.defineProperty(this, 'firstName', {
    get: function() {
        this.firstName = this.fullName[0];
        return this.firstName;
    }
  });

  Object.defineProperty(this, 'lastName', {
    get: function() {
        this.lastName = this.fullName[1];
        return this.lastName;
    }   
  });

}

var jone= new User("Jone Coven");

console.log(jone.fullName);
console.log(jone.firstName);
console.log(jone.lastName);

【问题讨论】:

    标签: javascript constructor get set


    【解决方案1】:

    问题在于this.firstName = ...this.lastName = ... 覆盖了已经使用Object.defineProperty(this, ...) 定义的属性。

    这是一个使用额外私有属性this._firstNamethis._lastName 的固定版本:

    function User(fullName) {
      this.fullName = fullName.split(' ');
    
      Object.defineProperty(this, 'firstName', {
        get: function() {
            this._firstName = this.fullName[0]; // <------ _firstName
            return this._firstName;
        }
      });
    
      Object.defineProperty(this, 'lastName', {
        get: function() {
            this._lastName = this.fullName[1]; // <----- _lastName
            return this._lastName;
        }   
      });
    
    }
    
    var jone= new User("Jone Coven");
    
    console.log(jone.fullName);
    console.log(jone.firstName);
    console.log(jone.lastName);
    

    另一种解决方案是立即返回结果:

    function User(fullName) {
      this.fullName = fullName.split(' ');
    
      Object.defineProperty(this, 'firstName', {
        get: function() {
            return this.fullName[0];
        }
      });
    
      Object.defineProperty(this, 'lastName', {
        get: function() {
            return this.fullName[1];
        }   
      });
    
    }
    
    var jone= new User("Jone Coven");
    
    console.log(jone.fullName);
    console.log(jone.firstName);
    console.log(jone.lastName);
    

    【讨论】:

    • 第二种解决方案正是我所需要的。现在我明白我在defineProperty 中做错了什么。谢谢朋友!
    【解决方案2】:

    为什么要复杂化?

    function User(fullName) {
      this.fullName = fullName.split(' ');
      this.firstName = this.fullName[0];
      this.lastName = this.fullName[1];
    }
    
    var jone= new User("Jone Coven");
    

    【讨论】:

    • 好的,谢谢。但在这种情况下,我必须仅通过 Object.defineProperty 描述符在构造函数中设置属性。 :(
    • 除非我遗漏了什么,否则只有在您使用 defineProperty 更改一些标准元数据(如可写、可配置等)时才有意义。或者如果您的 getter 和 setter 做的不仅仅是返回一个属性,它们都没有显示在您的代码中。因此,如果您选择 Dmitris 第二种解决方案,cdoshis 代码在功能上是相同的。
    【解决方案3】:
    function User(fullName) {
      this.fullName = fullName.split(' '); 
     this.firstName = function() {
            return this.fullName[0];
    
        }
    this.lastName = function() {
            return this.fullName[1];
    
        }
    }
    
    var jone= new User("Jone Coven");
    
    console.log(jone.fullName);
    console.log(jone.firstName());
    console.log(jone.lastName());
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多