특정 'ID'가있는 '구성원'개체가 컨테이너 'EntityGroup'개체의 '구성원'배열에 이미 있는지 확인하려고합니다. 왜 안 다음 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);
:-) – johowie