I have an object like this:
MyLibrary {
books:
[ Book {
bookType: 'romance'
}
]
}
Book is a class.
I would like to write a test to check if books contain an object that is an instance of Book. I was expecting to use something like MyLibrary.books.indexOf('Book') but it returns -1 even when Book exist.
asked Jul 18, 2017 at 11:52
1 Answer 1
You literally have instanceof
:
var hasBook = books.some((book) => book instanceof Book);
answered Jul 18, 2017 at 11:56
Alberto Trindade Tavares Alberto Trindade Tavares
10.4k6 gold badges42 silver badges49 bronze badges
Comments
lang-js
.some
elements inbooks
are aninstanceof
Book
...