เอาตัวแปรชื่อของเถียงกันของฟังก์ชัน TypeScript

0

คำถาม

ฉันอยากจะจัดการด่วนเรื่องง่ายที่จะทดสอบฟังก์ชันในสี่เหลี่ยมอยู่ไหนฉันก็แค่ข้อมูลที่ชื่อฟังก์ชันครั้งหนึ่งอยู่ใน constructor และทดสอบพวกเขา eg:

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

ปัญหากับเรื่องนี้มันเป็นถ้าฉันพยายามที่จะผ่านรกฟังก์ชันที่เถียงกันโดยไม่เรียกมัน,ฉันเข้าใจเป็น เรื่องนี้ ขอบเขตข้อผิดพลาด ยังไงฉันสามารถได้ทั้งสองอย่างที่ชื่อฟังก์ชันและมันกลับค่าตอนที่ฉันมองผ่านมันเข้าไปในการทดสอบฟังก์ชัน()โดยไม่ต้องประเภทสองอาร์กิวเมนต์?

มีตัวเลือกที่ดีกว่านี้บางทีอาจจะเป็นหน่วยงานการทดสอบ frameworks ให้เรื่องนี้?

ฉันเข้าไปหาปัจจุบันได้ผลแต่มันคือ clunky และฉันต้องการจะลบความต้องการสำหรับสองอาร์กิวเมนต์:

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ý
..................................................................................................................

ดังอยู่ในนี้หมวดหมู่

ดังคำถามอยู่ในนี้หมวดหมู่