HarmonyOS 中的 .ts 代码如何遍历对象?

​定义了一个类装饰器,想在类中的方法被调用时,打印方法的名字和参数。这段代码在 nodejs 中是可以正常运行的,但是在HarmonyOS中,遍历对象时,只能拿到对象的属性,对方的方法一个都拿不到,我 debug 后看到方法都在proto中,那为啥拿不到方法呢?

代码如下:​

export function LoggerAll<T extends new (...args: any[]) => any>(constructor: T) { 
  return class extends constructor { 
    constructor(...args: any[]) { 
      super(...args); 
      // 在构造函数中包装方法,确保每个实例都会进行包装 
      this.wrapMethods(); 
    } 
    private wrapMethods() { 
      for (const key in this) { 
        if (this.hasOwnProperty(key) && typeof this[key] === 'function') { 
          const originalMethod = this[key]; 
          this[key] = ((...args: any[]) => { 
            console.log(`Calling method: ${key}`); 
            console.log(`Arguments: `, args); 
            return originalMethod.apply(this, args); 
          }) as any; 
        } 
      } 
    } 
  }; 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
HarmonyOS
2024-11-25 09:20:45
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zbw_apple

ArkTS不允许使用标准库函数Function.apply、Function.bind以及Function.call。标准库使用这些函数来显式设置被调用函数的this参数。在ArkTS中,this的语义仅限于传统的OOP风格,函数体中禁止使用this。

export function LoggerAll<T extends new (...args: any[]) => any>(constructor: T) { 
  return class extends constructor { 
    constructor(...args: any[]) { 
      super(...args); 
      // 在构造函数中包装方法,确保每个实例都会进行包装 
      this.wrapMethods(); 
    } 
 
    private wrapMethods() { 
      let proto = Object.getPrototypeOf(this); 
      while (proto && proto !== Object.prototype) { 
        const keys = Object.getOwnPropertyNames(proto); 
        for (const key of keys) { 
          const descriptor = Object.getOwnPropertyDescriptor(proto, key); 
          if (descriptor && typeof descriptor.value === 'function' && key !== 'constructor') { 
            const originalMethod = descriptor.value; 
            proto[key] = (...args: any[]) => { 
              console.log(`Calling method: ${key}`); 
              console.log(`Arguments: `, args); 
              return originalMethod.apply(this, args); 
            }; 
          } 
        } 
        proto = Object.getPrototypeOf(proto); 
      } 
    } 
  }; 
} 
 
@LoggerAll 
class MyClass { 
  myMethod(arg1: string, arg2: number) { 
    console.log('Inside myMethod'); 
  } 
} 
const instance = new MyClass(); 
instance.myMethod('test', 123);
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.

使用 Object.getPrototypeOf 获取对象的原型链,并遍历原型链上的属性遍历原型链上的属性时,使用 Object.getOwnPropertyNames 获取所有属性名,而不是直接遍历对象本身的属性对于每一个属性,检查其是否是方法(typeof descriptor.value === ‘function’)并且不是构造函数(key !== ‘constructor’),然后进行方法包装。

分享
微博
QQ
微信
回复
2024-11-25 16:01:08


相关问题
HarmonyOS 如何遍历对象属性
687浏览 • 1回复 待解决
HarmonyOS ArkTS如何循环遍历对象
1225浏览 • 1回复 待解决
求大佬告知如何遍历JSON对象
1106浏览 • 1回复 待解决
HarmonyOS TS文件如何调用ArkTS代码
1024浏览 • 1回复 待解决
HarmonyOS 如何遍历获取page所有view
479浏览 • 1回复 待解决
HarmonyOS 如何遍历interface
556浏览 • 1回复 待解决
HarmonyOS 如何遍历resources/rawfile目录
572浏览 • 1回复 待解决
是否会长期支持ets调用ts代码
2284浏览 • 1回复 待解决
HarmonyOS 关于获取pixelmap对象代码示例
937浏览 • 1回复 待解决
HarmonyOS 如何遍历包含emoji字符串
1143浏览 • 1回复 待解决
HarmonyOS录音音频如何存放,以及遍历
1690浏览 • 1回复 待解决
worker.ts如何获取context
2777浏览 • 2回复 待解决