为什么要在Observable函数上调用.call()? [英] Why would you ever call .call() on Observable functions?

查看:143
本文介绍了为什么要在Observable函数上调用.call()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Angular的一个相对入门的人,我正在努力了解我从ng-bootstrap项目中读取的一些资料. 可在此处找到源代码.

I am a relative beginner in Angular, and I am struggling to understand some source I am reading from the ng-bootstrap project. The source code can be found here.

我对ngOnInit中的代码感到非常困惑:

I am very confused by the code in ngOnInit:

ngOnInit(): void {
    const inputValues$ = _do.call(this._valueChanges, value => {
      this._userInput = value;
      if (this.editable) {
        this._onChange(value);
      }
    });
    const results$ = letProto.call(inputValues$, this.ngbTypeahead);
    const processedResults$ = _do.call(results$, () => {
      if (!this.editable) {
        this._onChange(undefined);
      }
    });
    const userInput$ = switchMap.call(this._resubscribeTypeahead, () => processedResults$);
    this._subscription = this._subscribeToUserInput(userInput$);
  }

在这些Observable函数上调用.call(...)有什么意义?这种尝试要实现什么样的行为?这是正常模式吗?

What is the point of calling .call(...) on these Observable functions? What kind of behaviour is this trying to achieve? Is this a normal pattern?

作为我的Angular教育的一部分,我已经做了大量关于Observables的阅读/观看(无双关语),但是我从未遇到过这样的事情.任何解释将不胜感激

I've done a lot of reading/watching about Observables (no pun intended) as part of my Angular education but I have never come across anything like this. Any explanation would be appreciated

推荐答案

要了解它,首先您可以看一下预定义的JavaScript函数方法"call":

To understand it, first you can have a look at the predefined JavaScript function method "call":

var person = {
    firstName:"John",
    lastName: "Doe",
    fullName: function() {
        return this.firstName + " " + this.lastName;
    }
}
var myObject = {
    firstName:"Mary",
    lastName: "Doe",
}
person.fullName.call(myObject);  // Will return "Mary Doe"

调用"call"的原因是在对象"person"中调用一个函数并将上下文传递给"myObject".

The reason of calling "call" is to invoke a function in object "person" and pass the context to it "myObject".

类似地,在下面这样称呼呼叫"的原因:

Similarly, the reason of this calling "call" below:

const inputValues$ = _do.call(this._valueChanges, value => {
  this._userInput = value;
  if (this.editable) {
    this._onChange(value);
  }
});

提供上下文"this._valueChanges",但也提供基于该上下文调用的函数,即第二个参数,即匿名函数

is providing the context "this._valueChanges", but also provide the function to be called base on that context, that is the second parameter, the anonymous function

value => {
  this._userInput = value;
  if (this.editable) {
    this._onChange(value);
  }
}

在您使用的示例中:

  • this._valueChanges是可观察到的输入事件

  • this._valueChanges is the Input Event Observerable

_do.call用于在事件输入发生时产生一些副作用,然后返回源Observable(事件Observable)的镜像Observable

The _do.call is for doing some side affects whenever the event input happens, then it returns a mirrored Observable of the source Observable (the event observable)

已更新 示例代码: https://plnkr.co/edit/dJNRNI?p=preview

关于do呼叫:

您可以像这样在Observable上调用它:

You can call it on an Observable like this:

const source = Rx.Observable.of(1,2,3,4,5);
const example = source
.do(val => console.log(`BEFORE MAP: ${val}`))
.map(val => val + 10)
.do(val => console.log(`AFTER MAP: ${val}`));
const subscribe = example.subscribe(val => console.log(val));

在这种情况下,您不必将第一个参数作为上下文"Observable"传递.

In this case you don't have to pass the first parameter as the context "Observable".

但是,当您像上面所说的那样从其自己的位置调用它时,您需要将第一个参数传递为要调用的可观察".就是这样.

But when you call it from its own place like you said, you need to pass the first parameter as the "Observable" that you want to call on. That's the different.

正如@Fan Cheung所提到的,如果您不想在自己的地方调用它,可以这样做:

as @Fan Cheung mentioned, if you don't want to call it from its own place, you can do it like:

const inputValues$=this._valueChanges.do(value=>{
  this._userInput = value;
  if (this.editable) {
    this._onChange(value);
  }
})

这篇关于为什么要在Observable函数上调用.call()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆