Ask HN: (JavaScript): How often have you needed instanceOf and/or isProtoTypeOf?
I'm not questioning the utility of these, I'm just curious how often in day-to-day practice folks end up needing them.
if (obj instanceof Foo) obj.doSomething(); else if (obj instanceof Bar) obj.doSomethingElse();
In that case, if it makes sense for Foo and Bar to share a common ancestor, the ideal thing would be to have a doSomething function on the parent class (or parent prototype in javascript), which is overridden in both Foo and Bar, therefore making the type checking unnecessary. Simply call doSomething on the object.
You should note that this is not applicable to every situation, but it should take care of most cases where you would need to use instanceof.