I have two classes:
class Bar extends Foo { // Foo isn\'t relevant
constructor(value) {
if (!(value instanceof Foo)) throw \"InvalidArgumentException:
The problem is that all of your classes you reference are descendants of Foo
. Such that new Baz() instanceOf Bar && new Bar() instanceOf Foo === true
.
So when you ask is Bar instanceOf Foo, it will be true through inheritance.
Due to there being no Java getClass()
equivalent in JS, you should use something like:
if (value.constructor.name !== Foo.name)