Získajte názov premennej argument funkcie Strojom

0

Otázka

Ja by som chcel nastaviť rýchly jednoduchý spôsob, ako testu funkcie v Uhlovej, kde by som stačí zadať názov funkcie raz v konštruktér a testovať ich. napr.:

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

Problém s týmto je, že ak by som skúsiť prejsť funkciu ako argument bez volať to, mám to. rozsah chyba. Ako môžem dostať aj názov funkcie a jej návratovú hodnotu, keď som odovzdať ju do testu() funkcia, bez toho, aby zadajte dva argumenty?

Je tu lepšia možnosť, možno, že niektorí os linux poskytovať tento?

Môj súčasný prístup funguje, ale to je neohrabaný a chcel by som odstrániť potrebu druhý argument:

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

Najlepšiu odpoveď

1

Najspoľahlivejší spôsob, ako by sa prejsť reťazec, potom použite konzolu zápis sa pozrieť hore na this. Použitie triedy pole šípky funkciu, takže this kontext je správne.

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

Môžete napísať niečo nižšie (rozšírenie odpoveď @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

V iných jazykoch

Táto stránka je v iných jazykoch

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