问题描述
我是TypeScript的新手,但是我非常了解Java。在那里,每当需要满足功能接口时,通常都使用lambda表达式(->
)或方法引用(::
)完成。
Lambda表达式在两种语言之间似乎是等效的(如果我错了,请纠正我)。有没有办法利用方法引用?
当目标是实现这一目标时:
this.entryService.getEntries()
.subscribe(entries => this.listUpdateService.send(entries));
是否可以使用函数引用?以下操作方式似乎是错误的,因为send
不在this.listUpdateService
范围内执行。 (顺便说一句,它在哪个范围内执行?)
this.entryService.getEntries()
.subscribe(this.listUpdateService.send);
解决方法
您是对的,范围不是this.listUpdateService
。
如果要坚持正确的范围,通常使用bind
。
this.entryService.getEntries()
.subscribe(this.listUpdateService.send.bind(this.listUpdateService));