【安卓开发学习鸿蒙6】ArkTs箭头函数的使用和调用
·
class Person {
}
//Person对象可以用p指向
let p: Person = new Person()
//函数用什么变量可以指向它?
// p:函数类型=printC
// type 类型名=函数的定义
//自定义的一个函数类型,相当于一个函数的指针,或者叫引用
type myFunType = () => void
function tprintC() {
console.info('tttt')
}
let myBtnClickFun:myFunType=tprintC
type myFunType2 = (a: number) => void
function tprintC2(a: number) :string{
console.info('tttt' + a)
return ''+a
}
Button('测试按钮匿名函数', { type: ButtonType.Normal, stateEffect: true })
.onClick((event: ClickEvent) => {
let fn1 = () => {
console.info('箭头函数1')
}
fn1()
let fn2 = (a: number): number => {
console.info('箭头函数2');
return a;
}
let num = fn2(5)
console.info(`箭头函数${num}`);
let fn3: myFunType = tprintC //fn指向printC这个函数
fn3() //调用函数
let fn4: myFunType2 = tprintC2//fn指向printC这个函数
let result=fn4(5)//调用函数
console.info('aaa111=='+result)
})
Button('箭头函数的使用', { type: ButtonType.Normal, stateEffect: true })
.onClick(myBtnClickFun)
更多推荐



所有评论(0)