0
내가 CodeSchool의 운동 중 하나를 수행하고있어
나는 루프 "에 대한"를를 사용하고 싶어하지만 그들은 루프 정상을 사용하고 난 그런 식으로내 생각대로 (내 키 목록)이 작동하지 않는 이유는 무엇입니까?
var canyonCows = [
{name: "Bessie", type: "cow", hadCalf: "Burt"},
{name: "Bertha", type: "cow", hadCalf: null},
{name: "Donald", type: "bull", hadCalf: null},
{name: "Esther", type: "calf", hadCalf: null},
{name: "Burt", type: "calf", hadCalf: null},
{name: "Sarah", type: "cow", hadCalf: "Esther"},
{name: "Samson", type: "bull", hadCalf: null},
{name: "Delilah", type: "cow", hadCalf: null},
{name: "Shanaynay", type: "cow", hadCalf: null}
];
Object.prototype.noCalvesYet = function(){
//return true for an object if the object is a cow
if(this.hadCalf == null && this.type =='cow') return true;
else return false;
};
Array.prototype.countForBreeding = function(){
//this block doesn't work
var count = 0;
for(c in this)
{
if(c.noCalvesYet()) ++count;
}
return count;
//this block does work (when i comment out the above block, naturally)
// var count = 0;
// for(var i = 0; i < this.length;++i)
// {
// if(this[i].noCalvesYet())count++;
// }
// return count;
};
//find how many cows haven't had a calf yet and use those for breeding
alert(canyonCows.countForBreeding());
에게
내장 된 프로토 타입 객체에 이러한 함수를 넣는 것은 실제 생산 코드에서 명백하게 의심스러운 디자인 선택 일 것입니다. – Pointy
예. 감사합니다.) 좋은 생각은 아니지만 읽는 방법은 작동 방식의 기본 사항 만 가르치는 것입니다. 머리를 가져 주셔서 고마워. – user3389343