我正在尝试检查容器“EntityGroup”对象的“members”数组中是否已经存在具有特定“ID”的“member”对象。为什么以下 EntityGroup.idExists(id) 不起作用:
EntityGroup = function() {
this.members = []; // intention is for this to hold 'Entity' objects
this.classType = null; // what class of entities does it hold
};
EntityGroup.prototype = {
addEntity: function(entityType, EntityID) {
// TODO implement .idExists() check here
// dont add new member if the id does exist
this.members.push(new Entity(entityType, EntityID))
},
idExists: function(EntityID) {
var idExists = false,
member,
members = this.members;
for (member in members) {
if (EntityID == member.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 not expected
console.log(g.members);