简体   繁体   English

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

[英]How can I access constructor 'this' inside Ramda?

Whenever I try to access any this inside Ramda Compose function ( R.compose ) I get undefined , maybe it's because this is 'binded' to the Ramda Compose function. 每当我尝试访问任何this里面Ramda组合功能( R.compose )我得到undefined ,也许是因为this是“绑定”到Ramda组合功能。

How can I make this access something that is initiated in the Class constructor? 如何使this访问在Class构造函数中启动?

this.state is undefined inside getContent in the code below: 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)

The answer from Felix Kling is excellent. Felix Kling的答案非常好。 I want to add a little more context from Ramda, though. 不过,我想从Ramda中添加一些更多的上下文。

Ramda (disclaimer: I'm one of the authors) is about functional programming. Ramda(免责声明:我是其中一位作者)是关于函数式编程的。 It tries to do two things: to make it easier for Javascript developers to move towards more standard FP practices, and to make it easier for users of FP languages to work with Javascript. 它尝试做两件事:使Javascript开发人员更容易转向更标准的FP实践,并使FP语言的用户更容易使用Javascript。 There is no emphasis at all on interoperating with object-oriented styles of coding. 完全没有强调与面向对象的编码风格的互操作。

At one point Ramda did try to ensure that certain of its functions did maintain the this context, which would allow them to be used as OOP methods. 有一次,Ramda确实尝试确保其某些功能确实维护了this上下文,这将允许它们被用作OOP方法。 But we are dropping this focus entirely; 但我们完全放弃了这个焦点; it had always been speculative, without any requests at all for it, and when we accidentally broke it for some functions, we had no complaints at all. 它一直是投机的,没有任何要求,当我们不小心打破它的某些功能时,我们根本就没有任何抱怨。 There seems to be little reason for it. 似乎没有什么理由。 Meanwhile it complicates our implementation and hurts performance. 同时,它使我们的实施变得复杂并且伤害了性能。 So as we find the need to rewrite functions, we're no longer trying to ensure that this is maintained. 因此,当我们发现需要重写函数时,我们不再尝试确保维护它。

This makes sense. 这是有道理的。 Some people see Ramda as an alternative to Underscore or lodash, but that has always seemed skewed to us. 有些人认为Ramda是Underscore或lodash的替代品,但这似乎总是向我们倾斜。 Those libraries introduce some FP concepts, but they're designed to work in multi-paradigm environments, equally happy with imperative, OOP, or FP codebases. 这些库引入了一些FP概念,但它们被设计为在多范式环境中工作,同样满足于命令式,OOP或FP代码库。 Ramda is different, designed to work well only in functional systems. Ramda与众不同,旨在仅在功能系统中运行良好。 It's built entirely around the notion of building systems by composing pure functions. 它完全围绕构建系统的概念构建,通过组合纯函数。

For these reasons, on top of everything Felix said, there's no real reason to expect that a Ramda function will maintain your this context. 由于这些原因,除了Felix所说的一切之外,没有理由期望Ramda函数能够保持this情况。

It seems like you are using the public class fields proposals. 您似乎正在使用公共类字段提案。 Properties created this way are evaluated before the constructor itself is executed (step 8 and 11). 执行构造函数本身之前评估以这种方式创建的属性(步骤8和11)。

Ie your code is equivalent to 即你的代码相当于

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 clearly shows that you are trying to access this.state before it was initialized. 这清楚地表明您在初始化之前尝试访问this.state

Possible Solution 可能解决方案

Don't use the proposal and set the properties directly in the constructor, after you initialized 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()
  }
}

However , there is still an issue: The value assigned to getParents is a promise. 但是 ,仍然存在一个问题:分配给getParents的值是一个promise。 You cannot call a promise ( this.getParents() ). 你不能调用一个promise( this.getParents() )。 Maybe what you really want is to assign a function to getParents : 也许你真正想要的是为getParents分配一个函数:

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

And maybe R.concat doesn't return a function either, in which case this.getContent cannot be called either. 也许R.concat不返回函数或者,在这种情况下this.getContent也不能被调用。 In that case, what you actually want is 在那种情况下,你真正想要的是

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)
}

ie assign functions to getContent and getParents . 即将函数分配给getContentgetParents

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM