鸿蒙5.0开发【如何获取对象的所有方法】应用运维
·
可以使用Object.getOwnPropertyNames获取所有方法的字符串数组,注意获取对象的原型prototype需要文件后缀为.ts的文件,参考代码如下:
- 定义需要获取方法的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');
}
}
- 获取文件中的方法:
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%')
}
}
更多推荐



所有评论(0)