Difference between new Observable(…) and Rx.Observable.create(…)?

*爱你&永不变心* 提交于 2019-12-12 08:18:11

问题


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


回答1:


There is no difference. Observable.create calls new Observable.

As the manual says,

Observables are created using Rx.Observable.create or a creation operator

<...>

Rx.Observable.create is an alias for the Observable constructor

Observable.create is conventionally used, probably because it reads better in chains and conforms with other Observable static methods that create observables, too.

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.

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.



来源:https://stackoverflow.com/questions/45912735/difference-between-new-observable-and-rx-observable-create

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!