获得的可变名称的论点的功能。

0

的问题

我想设置一个快速简单的方法来测试职能角在哪里我只是输入的名称的功能,一旦在构造和测试。 例如:

constructor() {
    this.test(this.FucntionName)  <= logs function name and output
  }

问题是,如果我尝试通过一功能作为一个参数,而不调用它,我得到了 这一点。 范围的错误。 我如何可以得到两名称的功能及其返回值的时候我通过它进入测试()功能,没有必要类型的两个争论?

是有一个更好的选择,也许有些单元的测试框架提供的这个?

我目前的方法是有效的,但它是笨拙我想要删除的必要的第二个论点:

export class AuthService {
  constructor(
    ...
  ) {
    this.test(this.FunctionName(), 'FunctionName')// I would like to remove the need to repeat the name as string
  }

  test(sub: any, name) {
    sub.subscribe(
      data => {
        console.group("==========", name, "()    TEST  ===============")
        console.log("Output Type:", typeof data)
        typeof data == 'object' ? console.table(data) : console.log(data);
        console.groupEnd()
      }
    )
  }
angular typescript
2021-11-24 01:30:50
2

最好的答案

1

最可靠的方式将是通过串,然后使用括号标记来看,它在 this. 使用类领域箭头功能,这样的 this 下文是正确的。

test = (methodName) {
    this[methodName]().subscribe(
        data => {
            console.group("==========", methodName, "()    TEST  ===============")
            // ...
2021-11-24 01:35:55
0

你可写的东西喜欢在下面(延伸的答案@CertainPerformance)

export class AuthService {
  constructor(
    ...
  ) {
    this.test('FunctionName')
  }

  test(name) {
    this[name]().subscribe(
      data => {
        console.group("==========", name, "()    TEST  ===============")
        console.log("Output Type:", typeof data)
        typeof data == 'object' ? console.table(data) : console.log(data);
        console.groupEnd()
      }
    )
  }
2021-11-25 08:11:59

其他语言

此页面有其他语言版本

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................