I'm working on a highly object oriented project in JavaScript. I have an array[] of objects of different classes and I'm iterating through it and I want to check the class of the object at array[i]. Is there any way for me to detect this? This might help you understand what I mean:
var pieces = [new Pawn(), new Rook(), new Knight()];
for(var i = 0; i < pieces.length; i++){
if(pieces[i] == Rook){
//Do something with the rook
}
}
This is the general idea of what I need to do, but the if condition is the tricky bit. How can I detect the class of the element at pieces[i]? My fallback solution is to give every object a "type" field.