Javascript 使用不同类中的类对象

Javascript 使用不同类中的类对象,javascript,typescript,Javascript,Typescript,我有一个类型脚本类,它公开了一些服务列表 我定义了一个属性,该属性应该引用ServiceCatalog类,如下所示: export default class myGenerator extends Generator { private svcat: ServiceCatalog | undefined; // and here I’ve initilzied the property await this.getSvc(); // here I created some

我有一个类型脚本类,它公开了一些服务列表

我定义了一个属性,该属性应该引用
ServiceCatalog
类,如下所示:

export default class myGenerator extends Generator {

  private svcat: ServiceCatalog | undefined;


  // and here I’ve initilzied the property 

 await this.getSvc();

// here I created some function the return service instances
private async getSvc() {
  this.svcat = new ServiceCatalog();
  await this.svcat.getServiceInstances();
}


// and here I’ve additional code which use it 

this.svcat.values  ….
我的问题是,有一种更好/更干净的方法可以在javascript/typescript中实现这一点吗? 可能没有使用
这个
关键字


还有一个更好的测试代码(单元测试).

将服务注入myGenerator类。 将其添加到构造函数中:

    constructor(private svcat:ServiceCatalog) {}
现在,您可以使用

    await this.svcat.getServiceInstances();
无需为服务添加属性(svcat:ServiceCatalog |未定义部分)


“这个”在java/type脚本中非常需要,因为它指的是当前的类。

按照您今天的方式,很难进行测试。为什么呢?好吧,因为如果你想把你的
生成器
类与你的
服务目录
隔离开来,你会很困难

与上面的同事一样,我的建议是让客户提供的
ServiceCatalog
具有默认值

类MyGenerator扩展了生成器{
构造函数(私有svCat:ServiceCatalog=newServiceCatalog()){
超级();
}
}
这样你就可以像平常一样使用它了

newmygenerator()
或用于测试

新的MyGenerator(myFakeServiceCatalog)

对不起,这个问题我不清楚。您正在尝试初始化svCat并运行
getServiceInstances
?你能粘贴你班上的全部代码吗?现在,这不是一个有效的Typescript类。@Guillermevrs-是的,这就是我试图做的,这个类要长很多。。。什么是无效的
this.svcat.values….
我把它作为一个使用示例…@Guilhermevrs-我所做的是调用类(serviceCatalog)并将对象保存在
myGenerator
类中