检测对象是否是流类的实例的最佳方法是什么?

What is the best way to detect if an Object is an instance of a Stream Class?

本文关键字:最佳 方法 是什么 实例 检测 是否是 对象      更新时间:2023-09-26

是否有一种方法来检测对象是否是流类的实例?例如RxJS或Bacon.js流。

我要找的是像

这样的东西
function isStream(obj) {
   // if obj is RxJS or Bacon Stream return true, otherwise false
}

最可靠的方法是什么?

ObservableEventStreamProperty对象继承的基类。因此,如果您想检测任何培根,您可以使用Observable

function isStream(v) {
  return v instanceof Bacon.Observable
}
function test(v) {
  console.log(isStream(v))
}
test(Bacon.constant(1)) // true
test(Bacon.once(1))     // true
test(1)                 // false
http://jsbin.com/qugihobalu/2/edit

在每个框架中可能都有更好的方法,例如本地的isStream等价,但是检查instanceof是次优的解决方案,并且对bacon和rxjs都有效。

const isStream = x => x instanceof Bacon.Observable || x instanceof Rx.Observable;