HarmonyOS 鸿蒙Next arkTs在struct中this指向变化测试

发布于 1周前 作者 vueper 来自 鸿蒙OS

HarmonyOS 鸿蒙Next arkTs在struct中this指向变化测试 结论1:【箭头函数】内且【this在struct里】时this指向struct,其它情况都不再指向struct。

结果2:平时应习惯性写箭头函数,在不确定的调的方法是否可行时可以在外层定义let that,然后初始化that = this即可

image.png

let that

let myFunction_1 = function () {
  try {
    console.info('打印成功1 that', that.str)
    console.info('打印成功1', this.str)
  } catch (e) {
    console.error('打印失败1', JSON.stringify(e))
  }
}
let myFunction_3 = () => {
  console.info('打印成功3 that', that.str)
  console.error('打印失败3')
  // console.info('str', this.str) //这里编译器直接提示报错
}

@Entry
@Component
struct test {
  @State str: string = '输入内容'
  myFunction_2: Function = () => {
    try {
      console.info('打印成功2', this.str)
    } catch (e) {
      console.error('打印失败2', JSON.stringify(e))
    }
  }
  myFunction_4: Function = function () {
    try {
      console.info('打印成功4', this.str)
    } catch (e) {
      console.error('打印失败4', JSON.stringify(e))
    }
  }

  aboutToAppear() {
    that = this
  }

  build() {
    Column() {
      Button('测试a').onClick(() => {
        console.info('=======按钮 测试a')
        try {
          console.info('打印成功a', this.str)
        } catch (e) {
          console.error('打印失败a', JSON.stringify(e))
        }
        myFunction_1()
        this.myFunction_2()
        myFunction_3()
        this.myFunction_4()
      })
      Button('测试b').onClick(function () {
        console.info('========按钮 测试b')
        try {
          console.info('打印成功b', this.str)
        } catch (e) {
          console.error('打印失败b', JSON.stringify(e))
        }
        myFunction_1()
        try {
          this.myFunction_2()
        } catch (e) {
          console.error('打印失败b2', JSON.stringify(e))
        }
        myFunction_3()
        try {
          this.myFunction_4()
        } catch (e) {
          console.error('打印失败b2', JSON.stringify(e))
        }
      })
    }.width('100%').height('100%')
  }
}

更多关于HarmonyOS 鸿蒙Next arkTs在struct中this指向变化测试的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html

2 回复

在HarmonyOS鸿蒙Next中,arkTs(Ark TypeScript)是鸿蒙系统的一种开发语言,用于构建应用。在struct(结构体)中,this的指向问题是一个常见的关注点。通过测试可以观察到,this在arkTs中的行为与传统的TypeScript或JavaScript有所不同。

在arkTs中,this在struct中的指向通常是指向当前实例化的对象。然而,由于arkTs的编译和运行机制,this的指向可能会在某些情况下发生变化,特别是在异步操作或回调函数中。例如,在事件处理函数或异步任务中,this可能会丢失其原始指向,转而指向全局对象或其他上下文。

为了确保this的正确指向,开发者可以使用箭头函数来绑定this,因为箭头函数不会创建自己的this上下文,而是继承外层作用域的this。此外,arkTs也提供了bind方法,可以显式地将this绑定到特定的对象上。

在测试中,可以通过以下代码片段来验证this的指向:

struct MyStruct {
  name: string = "HarmonyOS";

  onClick() {
    console.log(this.name); // 正常情况下输出 "HarmonyOS"
  }

  onAsyncClick() {
    setTimeout(function() {
      console.log(this.name); // 可能输出 undefined,因为 this 指向了全局对象
    }, 1000);
  }

  onArrowClick() {
    setTimeout(() => {
      console.log(this.name); // 输出 "HarmonyOS",因为箭头函数继承了外层的 this
    }, 1000);
  }
}

通过上述测试,可以清晰地看到this在不同上下文中的指向变化。

更多关于HarmonyOS 鸿蒙Next arkTs在struct中this指向变化测试的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS的ArkTS中,this的指向在struct中通常指向当前组件的实例。你可以通过以下代码测试this的指向:

struct MyComponent {
  private name: string = "ArkTS";

  build() {
    Button(this.name)
      .onClick(() => {
        console.log(this); // 输出当前组件的实例
      });
  }
}

onClick回调中,this仍然指向MyComponent的实例,确保在事件处理中能够正确访问组件的属性和方法。

回到顶部
AI 助手
你好,我是IT营的 AI 助手
您可以尝试点击下方的快捷入口开启体验!