可以使用Object.getOwnPropertyNames获取所有方法的字符串数组,注意获取对象的原型prototype需要文件后缀为.ts的文件,参考代码如下:

  1. 定义需要获取方法的class文件,testClass.ts:
export class testClass {
  public test(): string {
    return "ArkUI Web Component";
  }

  public toString(): void {
    console.log('Web Component toString');
  }

  public FunToString(): void {
    console.log('Web Component toString');
  }
}
  1. 获取文件中的方法:
import { testClass } from './testClass';

let protoType = testClass.prototype;
let methodsName: string[] = Object.getOwnPropertyNames(protoType);
console.info(methodsName.toString());

@Entry
@Component
struct GetObjectAllFun {
  @State message: string = 'Hello World';

  build() {
    Row() {
      Column() {
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
      }
      .width('100%')
    }
    .height('100%')
  }
}
Logo

讨论HarmonyOS开发技术,专注于API与组件、DevEco Studio、测试、元服务和应用上架分发等。

更多推荐