【问题标题】:Create a Class with mandatory and optional properties创建具有强制和可选属性的类
【发布时间】:2018-08-20 06:35:17
【问题描述】:

我正在为我的 Express Web 应用程序定义一个 ECMAScript 6 类。它有一个强制属性和一些可选属性。必填字段需要在类构建期间分配,而可选字段则不需要。

可选属性只需要按需设置。

class Item{

    constructor(mandatoryField) {
        this._mandatoryField = mandatoryField;
        this._optionalField1 = undefined;
        this._optionalField2 = undefined;
        this._optionalField3 = undefined;
    }

    get mandatoryField() {
        return this._mandatoryField;
    }

    set mandatoryField(newVal){
        if(newVal){ 
            this._mandatoryField = newVal;
        }
    }

    get optionalField1() {
        return this._optionalField1;
    }

    set optionalField1(newVal){
        if(newVal){ 
            this._optionalField1 = newVal;
        }
    }

    get optionalField2() {
        return this._optionalField2;
    }

    set optionalField2(newVal){
        if(newVal){ 
            this._optionalField2 = newVal;
        }
    }

    get optionalField3() {
        return this._optionalField3;
    }

    set optionalField3(newVal){
        if(newVal){ 
            this._optionalField3 = newVal;
        }
    }
}

var item1 = new Item("mandVal");
item1.mandatoryField;    // access the mandatory property
item1.optionalField1("optVal");    // set the optional property
item1.optionalField1;    // access the optional property

上面的声明有问题吗?如何使可选属性在类声明中具有默认值?

【问题讨论】:

  • 而不是 assining undefined ,分配默认值
  • 您需要分配哪个默认值?
  • 你需要这些setter和getter做什么?它们似乎完全是多余的,并且忽略了虚假值的分配。
  • @Bergi。构造对象后,需要 setter 和 getter 来分配和访问属性。
  • 类初始化时可选属性未知。他们将在以后分配。

标签: javascript node.js express ecmascript-6


【解决方案1】:

我认为你可以简化为

class Item {
    constructor(mandatoryField) {
        this.mandatoryField = mandatoryField;
        this.optionalField1 = undefined; // this `undefined` is the default value
        this.optionalField2 = undefined;
        this.optionalField3 = undefined;
    }
}

var item1 = new Item("mandVal");
console.log(item1.mandatoryField); // access the mandatory property
console.log(item1.optionalField1); // access an optional property
item1.optionalField1 = "optVal";  // set the optional property
console.log(item1.optionalField1); // access the optional property

【讨论】:

  • 感谢您的示例。我对 ECMA6 很陌生。无论如何,我的代码也可以工作,但不仅需要 getter 和 setter。根据 mozilla 开发者文档,getter 和 setter 是用于创建和设置伪属性的函数。
【解决方案2】:

在构造函数之前创建可选变量并使用如下:

export class Item{

    _optionalField1 = 'some value';
    _optionalField2 = 'some value';
    _optionalField3 = 'some value';
    _mandatoryField = 'some value';
    
        constructor(mandatoryField) {
            this._mandatoryField = mandatoryField;
        }
    
        get mandatoryField() {
            return this._mandatoryField;
        }
    
        set mandatoryField(newVal){
            if(newVal){ 
                this._mandatoryField = newVal;
            }
        }
    
        get optionalField1() {
            return this._optionalField1;
        }
    
        set optionalField1(newVal){
            if(newVal){ 
                this._optionalField1 = newVal;
            }
        }
    
        get optionalField2() {
            return this._optionalField2;
        }
    
        set optionalField2(newVal){
            if(newVal){ 
                this._optionalField2 = newVal;
            }
        }
    
        get optionalField3() {
            return this._optionalField3;
        }
    
        set optionalField3(newVal){
            if(newVal){ 
                this._optionalField3 = newVal;
            }
        }
    }
    
    var item1 = new Item("mandVal");
    item1.mandatoryField;    // access the mandatory property
    item1.optionalField1("optVal");    // set the optional property
    item1.optionalField1;    // access the optional property

【讨论】:

  • 最好的做法是也分配变量类型,因此应该在定义变量时添加。
  • 这看起来不像是有效的 ES8 语法
  • @TarunKhurana 我在 jsfiddle 中运行了您的代码,但出现错误。 Class properties must be methods. Expected '(' but instead saw '='.
  • 您是否也复制了最后 4 行,如果是,请删除该行,这些是使用此类的示例行。
猜你喜欢
  • 2018-12-02
  • 2021-02-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-01
  • 2012-06-17
  • 1970-01-01
相关资源
最近更新 更多