如何在 Ramda 中访问构造函数 'this'?

How can I access constructor 'this' inside Ramda?

每当我尝试访问 Ramda Compose 函数 (R.compose) 中的任何 this 时,我得到 undefined,可能是因为 this 是 'binded' Ramda Compose 函数。

如何让 this 访问在 Class 构造函数中启动的内容?

this.state 在下面的代码中 getContent 内未定义:

export default class FaqStore {
  constructor() {
    this.state = new FaqState()

    this.getParents()
  }

  getContent = R.concat(this.state.parents, R.prop('content'))
  getParents = FaqService.getParents().then(this.getContent)

您似乎正在使用 public class 字段提案。以这种方式创建的属性是 evaluated before the constructor itself is executed(步骤 8 和 11)。

即你的代码相当于

export default class FaqStore {
  constructor() {
    this.getContent = R.concat(this.state.parents, R.prop('content'))
    this.getParents = FaqService.getParents().then(this.getContent)

    this.state = new FaqState()

    this.getParents()
  }
}

这清楚地表明您正试图在 this.state 初始化之前访问它。

可能的解决方案

不要使用建议并直接在构造函数中设置属性, 初始化后this.state:

export default class FaqStore {
  constructor() {
    this.state = new FaqState()

    this.getContent = R.concat(this.state.parents, R.prop('content'))
    this.getParents = FaqService.getParents().then(this.getContent)

    this.getParents()
  }
}

但是,还有一个问题:赋给getParents的值是一个promise。您不能 为承诺 (this.getParents())。也许您真正想要的是将函数分配给 getParents:

this.getParents = () => FaqService.getParents().then(this.getContent)

也许 R.concat 也不是 return 函数,在这种情况下 this.getContent 也不能被调用。在那种情况下,你真正想要的是

export default class FaqStore {
  constructor() {
    this.state = new FaqState()

    this.getParents()
  }

  getContent = () => R.concat(this.state.parents, R.prop('content'))
  getParents = () => FaqService.getParents().then(this.getContent)
}

即将 函数 分配给 getContentgetParents.

Felix Kling 的回答非常好。不过,我想从 Ramda 添加更多上下文。

Ramda(免责声明:我是作者之一)是关于函数式编程的。它试图做两件事:让 Javascript 开发人员更容易转向更标准的 FP 实践,并让 FP 语言的用户更容易使用 Javascript。完全没有强调与面向对象的编码风格进行互操作。

在某一时刻,Ramda 确实尝试确保它的某些函数确实维护了 this 上下文,这将允许它们被用作 OOP 方法。但是我们完全放弃了这个重点;它一直是推测性的,根本没有任何要求,当我们不小心将它用于某些功能时,我们也没有任何抱怨。似乎没有什么理由这样做。同时,它使我们的实施变得复杂并损害了性能。因此,当我们发现需要重写函数时,我们不再试图确保它得到维护。

这是有道理的。有些人将 Ramda 视为 Underscore 或 lodash 的替代品,但在我们看来,这似乎总是有偏差。这些库引入了一些 FP 概念,但它们设计用于在多范式环境中工作,同样适用于命令式、OOP 或 FP 代码库。 Ramda 不同,它被设计成只在函数式系统中运行良好。它完全围绕通过组合纯函数构建系统的概念构建。

出于这些原因,除了 Felix 所说的一切之外,没有真正的理由期望 Ramda 函数会维护您的 this 上下文。