简体   繁体   English

设置boolean typescript的默认值

[英]Set default value of boolean typescript

I have an object: 我有一个对象:

export class ReccurrenceModel {
    fromDate: Date;
    toDate: Date;
    weeklyReccurrence: number;
    state: State;
    isMonday: boolean;
    isTuesday: boolean;
    isWednesday: boolean;
    isThursday: boolean;
    isFriday: boolean;
    fromDateToReturn: Date;
    toDateToReturn: Date;
}

I use it like this 我这样用它

  if (this.reccurrenceSelected === true) {
      this.reccurrence.isMonday = this.mondaySelected;
      this.reccurrence.isTuesday = this.tuesdaySelected;
      this.reccurrence.isWednesday = this.wednesdaySelected;
      this.reccurrence.isThursday = this.thursdaySelected;
      this.reccurrence.isFriday = this.fridaySelected;
}

I want to set a default value for them - false because if I do not set them in in UI, they will be undefined and I don't want that. 我想为它们设置一个默认值 - false,因为如果我没有在UI中设置它们,它们将是未定义的,我不希望这样。

How to set de default value of a boolean in typescript? 如何在typescript中设置布尔值的默认值?

undefined , as well as false , are both falsy values that you can test the same way. undefinedfalse都是false值,你可以用同样的方式测试。

But default values are set with 但默认值设置为

export class ReccurrenceModel {
    fromDate: Date;
    toDate: Date;
    weeklyReccurrence: number;
    state: State;
    isMonday = false;
    isTuesday = false;
    ...
    fromDateToReturn: Date;
    toDateToReturn: Date;
}

Doesn't make any big change in UI level you can use either. 您也可以使用UI级别的任何重大更改。 Both are falsy values for UI. 两者都是UI的虚假值。

You can set anyone. 你可以设置任何人。

variableName = false 

or 要么

variableName: boolean;

variableName you can use either UI consider it as false by default untill you assign its value to true. variableName您可以使用UI认为它是假的默认情况下,直到将其值指定为true。

I would add a default constructor and do something like this : 我会添加一个默认构造函数,并执行以下操作:

export class ReccurrenceModel {
    fromDate: Date;
    toDate: Date;
    weeklyReccurrence: number;
    state: State;
    isMonday: boolean;
    isTuesday: boolean;
    isWednesday: boolean;
    isThursday: boolean;
    isFriday: boolean;
    fromDateToReturn: Date;
    toDateToReturn: Date;

    constructor(){
      this.isMonday = false;
      this.isTuesday = false;
      this.isWednesday = false;
      this.isThursday = false;
      this.isFriday = false;
    }
}

later i would do something like this : , 以后我会做这样的事情:

this.reccurrence = new ReccurrenceModel();

The above line would initialize the required fields. 上面的行将初始化必填字段。 you can confirm this by doing a console.log(this.reccurrence) after calling the constructor 你可以在调用constructor后通过执行console.log(this.reccurrence)来确认这一点

I would suggest to use getters and setters in class 我建议在课堂上使用getterssetters

export class ReccurrenceModel {
    fromDate: Date;
    toDate: Date;
    weeklyReccurrence: number;
    state: State;
    isMonday: boolean;
    isTuesday: boolean;
    isWednesday: boolean;
    isThursday: boolean;
    isFriday: boolean;
    fromDateToReturn: Date;
    toDateToReturn: Date;
        ...

        get fromDate() {
            return this.fromDate|| "";
        }
        get isMonday() {
            return this.isMonday|| false;
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM