创建具有强制和可选属性的类

时间:2018-08-20 06:35:17

标签: javascript node.js express ecmascript-6

我正在为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

上述声明是否有问题?如何在类声明中使可选属性具有默认值?

2 个答案:

答案 0 :(得分:0)

我认为您可以简化为

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

答案 1 :(得分:-1)

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

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

相关问题