1.用箭头函数,箭头函数不绑定自己的this,它会捕获其所在上下文的this值作为自己的this值
class A{
a:string = "a";
funA() {
let b: B = {
funC:() =>{
console.log(this.a)
this.funB()
}
}
}
funB() {
}
}
interface B {
funC: () => void;
}
2.用bind函数进行绑定,在arkts中限制了bind的使用,在使用bind时会有警告
class A {
value: string = ''
foo: Function = () => {}
}
class Test {
value: string = '1234'
obj: A = {
value: this.value,
foo: this.foo.bind(this)
}
foo() {
console.log(this.value);
}
}
03-29
1547
07-14
511