新的Observable(...)和Rx.Observable.create(...)之间的区别? [英] Difference between new Observable(...) and Rx.Observable.create(...)?
问题描述
我正在更新我们的软件,将所有的Promise(以及其他有毛的垃圾)替换为可观察的东西.为了确保遵循最佳做法,我做了一个快速的googlearch并注意到在某些情况下,建议的语法是根据实例的,而在其他情况,这些示例按工厂执行调用.
I'm updating our software substituting all promises (and other hairy junk) for observables. To make sure that I'm following best practices, I made a quick googlearch and noticed that in some cases, the suggested syntax is by instance whereas in other cases, the examples perform a call by factory.
const byInstance = new Observable(_ => { ... });
const byFactory = Rx.Observable.create(_ => { ... });
我很好奇实际的区别是什么.它们可互换吗?它是较旧/较新的语法/方法吗?它与框架相关吗?而且,当然,这是首选(在没有理由,有争议的情况下).
I'm curious what's the actual difference. Are they precisely interchangeable? Is it an older/newer syntax/approach? Is it framework related? And, of course, which is to be preferred (under the condition that it's not opinionated, disputed etc.).
推荐答案
没有区别. Observable.create
呼叫new Observable
There is no difference. Observable.create
calls new Observable
.
如手册所述,
可观察对象使用Rx.Observable.create或创建来创建 运算符
Observables are created using Rx.Observable.create or a creation operator
< ...>
Rx.Observable.create是Observable构造函数的别名
Rx.Observable.create is an alias for the Observable constructor
通常使用
Observable.create
,可能是因为它在链中的读取效果更好,并且也与创建可观察对象的其他Observable
静态方法一致.
Observable.create
is conventionally used, probably because it reads better in chains and conforms with other Observable
static methods that create observables, too.
差异可能出现在子类中.例如,Subject.create
等于AnonymousSubject.create
且不等于new Subject
.通常,Subject.create
是提供所需行为的程序,而new Subject
是更底层的程序.这证实了有关约定的要点.
The difference may appear in child classes. For example, Subject.create
is equal to AnonymousSubject.create
and not equal to new Subject
. Usually Subject.create
is the one that provides desirable behavour, while new Subject
is more low-level. This confirms the point about the convention.
另一方面,某些类(尤其是BehaviorSubject
)应该与new
一起使用,因为create
签名不允许向其提供所需的行为.
On the other hand, some classes (notably BehaviorSubject
) are supposed to be used with new
because create
signature doesn't allow to provide the desired behaviour to them.
这篇关于新的Observable(...)和Rx.Observable.create(...)之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!