Oct*_*bin 8 java lambda scope typescript method-reference
我对 TypeScript 有点陌生,但我对 Java 很了解。每当需要满足函数接口时,最常见的是使用 lambda 表达式 ( ->) 或方法引用 ( ::) 来完成。
Lambda 表达式在两种语言之间似乎是等效的(如果我错了,请纠正我)。有没有办法利用方法引用?
当目标是实现这一点时:
this.entryService.getEntries()
.subscribe(entries => this.listUpdateService.send(entries));
Run Code Online (Sandbox Code Playgroud)
有没有办法使用函数引用?下面的做法似乎是错误的,因为send不在 的范围内执行this.listUpdateService。(顺便说一句,它在哪个范围内执行?)
this.entryService.getEntries()
.subscribe(this.listUpdateService.send);
Run Code Online (Sandbox Code Playgroud)
你是对的,范围不是this.listUpdateService。
如果您想坚持正确的范围,通常可以使用bind.
this.entryService.getEntries()
.subscribe(this.listUpdateService.send.bind(this.listUpdateService));
Run Code Online (Sandbox Code Playgroud)