目录

How do I use Reflect to correctly bind the this pointer in ArkTS?

Refer to the code snippet below. Note that only the get and set methods of an object can be bound to the this pointer.

class ReflectClass {
  private a = 'a';

  get getA() {
    return () => {
      return this.a;
    };
  }

  set setA(a: string) {
    this.a = a;
  }
}

function testInvoke() {
  const reflectClass = new ReflectClass();
  const fn: Function = Reflect.get(reflectClass, 'getA', reflectClass);
  console.info(fn());
}

@Entry
@Component
struct ReflectBoundThis {
  aboutToAppear(): void {
    testInvoke();
  }

  build() {
  }
}