该函数设计用于计算具有类型的对象:cow和hadCalf:null并将值存储到numToBreed,我总是在控制台中获得零。为什么?Javascript布尔和原型
var forestCows = [
{name: "Marcus", type: "calf", hadCalf: null},
{name: "Isaiah", type: "bull", hadCalf: null},
{name: "Lemuel", type: "cow", hadCalf: null},
{name: "Daniel", type: "cow", hadCalf: null},
{name: "Joseph", type: "cow", hadCalf: "Legolas"}
];
Object.prototype.noCalvesYet=function(){
if(this.type=="cow" && this.hadCalf==null){
return true;}
else{return false;}
};
Array.prototype.countForBreeding=function(){
var numToBreed=0;
for(var i=0;i<this.length;i++){
this[i].noCalvesYet();
if(this[i].noCalvesYet===true){
numToBreed++;
}
}
return numToBreed;
};
var store=forestCows.countForBreeding();
console.log(store);
而且我试图创建一个新的功能,以更好地了解内有条件的布尔“如果”
var executeThis=function(){return true;};
executeThis();
if(executeThis===true){console.log(3+5);}
在这种情况下,没有什么是控制台中显示,为什么?
`
`
你能解释为什么它不起作用吗?我首先调用了这个[i] .noCalvesYet(),并且由于返回了noCalvesYet的值(true或false),那么它应该可以工作。我需要一个解释为什么它不起作用 –