TypeScript等效于Java方法参考

问题描述

我是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));

Here is an example playground

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...