I am trying to verify the existence of a 'member' object with a specific 'ID' in the 'members' array of an 'EntityGroup' container object. I'm having trouble understanding why the EntityGroup.idExists(id) method is not functioning as expected:
EntityGroup = function() {
this.members = []; // intended to store 'Entity' objects
this.classType = null; // specifies the class type of entities it holds
};
EntityGroup.prototype = {
addEntity: function(entityType, EntityID) {
// TODO include .idExists() check here
// do not add new member if the id already exists
this.members.push(new Entity(entityType, EntityID))
},
idExists: function(EntityID) {
var idExists = false,
member,
members = this.members;
for (var i = 0; i < members.length; i++) {
if (EntityID == members[i].EntityID) {
idExists = true;
break;
} else {
continue;
}
}
return idExists;
}
};
Entity = function(entityType, EntityID) {
this.EntityID = EntityID;
this.entityType = entityType;
};
g = new EntityGroup();
g.addEntity("Person", 1);
g.addEntity("Person", 2);
console.log(g.idExists(1)); // returns false, which is unexpected
console.log(g.members);