简体   繁体   English

TypeScript类中的命名空间或类别

[英]Namespaces or categories in TypeScript class

I have a base class with methods that access different kind of objects: 我有一个带有访问不同类型对象的方法的基类:

- findPropertyAInItemY(items)
- getPropertyBInItemZ()
- getItemY()
- getAlltems()

they sometimes access private class variables, sometimes not. 他们有时访问私有类变量,有时不访问。 The methods have sometimes parameter, sometimes not. 这些方法有时带有参数,有时没有。 There are different cases... 有不同的情况...

These method names become very long but they describe exactly what they return. 这些方法名称变得很长,但它们准确描述了它们返回的内容。 But there a lot of methods. 但是有很多方法。 Therefor I would like to have a structure like: 因此,我想有一个像这样的结构:

- Properties.getPropertyA()
- Properties.findPropertyB()
- Items.getItemY()
- Item.getAllItems()

in my base class. 在我的基础课上。 Is this somehow possible? 这可能吗?

I'm not a hundred percent certain what you want to achieve, but you can get what you're asking in several ways. 我不确定要实现的目标是百分之一百,但是您可以通过几种方式获得所要的内容。 Although the static version looks exactly like what you are asking for, it is usually better to accept an instance in your constructor of a class that will satisfy the dependency (but that's expanding the question a fair bit). 尽管static版本看起来完全符合您的要求,但是通常最好在类的构造函数中接受一个实例,该实例将满足依赖关系(但这在很大程度上扩展了问题)。

class Properties {
  getPropertyA() {

  }

  findPropertyB() {

  }

  static getPropertyC() {

  }
}

class Example {
  private properties = new Properties();

  example() {
    this.properties.getPropertyA();
  }

  example2() {
    Properties.getPropertyC();
  }
}

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

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