我是TypeScript的新手,但我对Java很了解。在那里,每当需要满足函数接口时,最常见的方法是使用lambda表达式(->
(或方法引用(::
(。
Lambda表达式在这两种语言之间似乎是等价的(如果我错了,请纠正我(。有没有办法利用方法引用?
当目标是实现这一点时:
this.entryService.getEntries()
.subscribe(entries => this.listUpdateService.send(entries));
有没有办法使用函数引用?以下方法似乎是错误的,因为send
没有在this.listUpdateService
的范围内执行。(BTW,它在哪个范围内执行?(
this.entryService.getEntries()
.subscribe(this.listUpdateService.send);
您是对的,范围不是this.listUpdateService
。
如果你想坚持正确的范围,你通常使用bind
。
this.entryService.getEntries()
.subscribe(this.listUpdateService.send.bind(this.listUpdateService));
下面是一个操场的例子