报错情况说明:
在方法A中添加setInterval方法。其中第一个参数为一个方法( 方法B )(这种情况的报错可以类比到所有Lambda函数),第二个参数为时间。
方法B中有调用了其他方法(方法C)
此时报错ERROR TypeError: this.functionB is not a function
代码为:
init(): void {
setInterval(this.functionA, 1000);
}
functionA() {
this.functionB('1');
this.functionB('2');
}
functionB(name: string): void {
console.log('functionB' + name);
}
解决:在setInterval方法调用A方法时添加 .bind(this)
init(): void {
setInterval(this.functionA.bind(this), 1000);
}
functionA() {
this.functionB('1');
this.functionB('2');
}
functionB(name: string): void {
console.log('functionB' + name);
}