为什么我们在下面的代码中使用两个过滤器而不是一个?
fromEvent<MouseEvent>(this.mapElement, "click")
.pipe(
filter(e => !this.props.disableEvents),
filter(_ => !this.mouseState.moved && mouseDownInMap)
)
.subscribe(e => {});
为什么不:
fromEvent<MouseEvent>(this.mapElement, "click")
.pipe(filter( e => !this.props.disableEvents && !this.mouseState.moved && mouseDownInMap))
.subscribe(e => {});
另外,为什么我们需要
.pipe()
如果它也可以在没有管道的情况下工作?
最佳答案
您可以将它们组合成一个 filter
.
但是为了代码维护,通常更容易阅读两个单独的过滤器函数,特别是如果它们在概念上不相关 - 并避免水平滚动。
另一个原因是过滤器函数本身可能在别处定义,在这种情况下,您必须使用单独的 filter
无论如何调用:
例如
function whenEventsAreEnabled( e ) {
return !this.props.disableEvents;
}
function whenMouseIsInMap( e ) {
!this.mouseState.moved && mouseDownInMap
}
fromEvent<MouseEvent>(this.mapElement, "click")
.pipe(
filter( whenEventsAreEnabled ),
filter( whenMouseIsInMap )
)
.subscribe(e => {});
Also, why we need
.pipe()
if it works withoutpipe
too?
你 做需要
pipe
.您只能在不使用 pipe
的情况下逃脱。如果你在 backwards compatibility mode 中使用 RxJS . Modern RxJS always requires pipe()
to create a pipeline for the observable's emitted values/objects .
关于javascript - 为什么我们使用两个过滤器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60504849/