TypeScript:具有构造函数的类,该构造函数根据传入的参数调用方法 [英] TypeScript: class with a constructor which calls methods depending incoming parameter

查看:59
本文介绍了TypeScript:具有构造函数的类,该构造函数根据传入的参数调用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个普通的TS类,看起来像这样:

I have a regular TS class looking like this:

export class Company {
  private firmId: number;
  private regNr: string;
  private firmName: string;
  private address: string;
  private status: string;
  private town: string;
  private zip: string;
  private postAddress: string;
  private tel1: string;
  private tel2: string;
  private fax: string;
  private eMail: string;
  private homepage: string;
  private webshow: string;
  private bankcode: string;
  private bankaccount: string;
  private contact: string;
  private addidata: string;
  private entryDate: string;
  private userId: string;
  private infoEMail: string;
  private pfId: string;
  private pfName: string;
  private country: string;

  constructor(company: any) {
    this.firmId = company.firmId;
    this.regNr = company.regNr;
    this.firmName = company.firmName;
    this.address = company.address;
    this.status = company.status;
    this.town = company.town;
    this.zip = company.zip;
    this.postAddress = company.postAddress;
    this.tel1 = company.tel1;
    this.tel2 = company.tel2;
    this.fax = company.fax;
    this.eMail = company.eMail;
    this.homepage = company.homepage;
    this.webshow = company.webshow;
    this.bankcode = company.bankcode;
    this.bankaccount = company.bankaccount;
    this.contact = company.contact;
    this.addidata = company.addidata;
    this.entryDate = company.entryDate;
    this.userId = company.userId;
    this.infoEMail = company.infoEMail;
    this.pfId = company.pfId;
    this.pfName = company.pfName;
    this.country = company.country;
  }
}

这个构造函数显然很胖,我正在考虑将其重构为构造器模式,但是现在它是这样的.

This constructor is obviously very fat and I am considering refactoring to builder pattern, but for now it is like this.

此类通过JSON响应实例化,该响应具有绝对相同的字段结构.

This class is instantiated with a JSON response, that has absolutely the same field structure.

当我需要使用空值实例化此类以使Angular表单验证正常工作时,就会出现问题.

The problem arises when I need to instantiate this class with empty values in order for Angular form validation to work correctly.

我该如何实现?我可以创建一个构造函数,该构造函数根据构造函数的参数来调用此类的方法,如下所示:

How can I achieve that? Can I create a constructor which calls the methods of this class depending upon the constructor parameters, something like this:

export class Company {
  // list of fields ...
  constructor(company: any) {
    if (company != '') {
      this.instantiateEmpty();
    } else {
      this.instantiateWithData();
    }
  }

  private instantiateEmpty() {
    // create empty fields of class
  }

  private instantiateWithData() {
    // create filled fields
  }
}

还是我应该使用类似于生成器的方法来创建此类,并根据我对类的需要使用正确的静态方法:使用数据实例化还是使用空字段实例化?

Or should I create this class with a builder-like approach and just use correct static method depending upon what I need to do with the class: instantiate with data or instantiate with empty fields?

谢谢!

推荐答案

让我们从

Let's start with Object.assign which will make your constructor very slim:

constructor(company: any) {
    Object.assign(this, company);
}

仅当如您所写的那样, company 对象与类具有相同的字段时,该函数才有效.

It will work only if, as you wrote, the company object has the same fields as the class.

现在,您还可以为构造函数使用不同的签名:

Now, you can also have a different signature to your constructor:

constructor();
constructor(company: any);
constructor(company?: any) {
    if (comapny) {
        Object.assign(this, company);
    } else {
        ....
    }
}


编辑

为了初始化所有具有空值的字段,我建议使用一个常量对象,该对象具有与类完全相同的字段,但具有空字段,然后将其与 Object.assign 一起使用,像:

const DEFAULT_VALUES = {
    firmId: 0,
    regNr: "",
    ...
}

class Company {
    constructor();
    constructor(company: any);
    constructor(company?: any) {
        if (comapny) {
            Object.assign(this, company);
        } else {
            Object.assign(this, DEFAULT_VALUES);
        }
    }
}

甚至只是:

class Company {
    constructor(company: any = DEFAULT_VALUES) {
        Object.assign(this, company);
    }
}

这篇关于TypeScript:具有构造函数的类,该构造函数根据传入的参数调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆