2017-04-22 87 views
0

该函数设计用于计算具有类型的对象: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);} 

在这种情况下,没有什么是控制台中显示,为什么?

`

`

回答

3

试试这个:

... 
for(var i=0;i<this.length;i++){ 
    if(this[i].noCalvesYet()===true){ 
    numToBreed++; 
    } 
} 
... 

错误在这里,这不叫方法noCalvesYet:

if(this[i].noCalvesYet===true) 
+1

你能解释为什么它不起作用吗?我首先调用了这个[i] .noCalvesYet(),并且由于返回了noCalvesYet的值(true或false),那么它应该可以工作。我需要一个解释为什么它不起作用 –

1

问题是与if语句,更改:

if(this[i].noCalvesYet === true) { 
    numToBreed++; 
} 

到:

if(this[i].noCalvesYet() === true){ 
    numToBreed++; 
} 

您在检查对象的属性,而不是函数结果。

+0

如果我把一个函数放在一个对象中,它可以采用布尔值true或false吗? –

+0

设置属性是不同的东西,然后返回值。您不能拥有相同的属性名称和方法名称。要缓存值,您可以使用不同的属性并在方法内分配它。但在你的例子中它没有任何意义。如果操作员只需要调用方法一次。 –

1

函数调用不会将其返回值存储回自己。您需要立即使用返回值,方法是将其分配给变量,或直接在需要该值的地方使用函数调用。换句话说之一:

var executeThis = function() { 
 
    return true; 
 
}; 
 

 
var result = executeThis(); 
 

 
if(result === true) { 
 
    console.log(3+5); 
 
}

或者跳过变量,并调用在同一时间的功能,当你测试它的返回值。

var executeThis = function() { 
 
    return true; 
 
}; 
 

 
if(executeThis() === true) { 
 
    console.log(3+5); 
 
}

令牌executeThis没有括号总是函数本身的引用,从来没有它的返回值。

+0

谢谢,这解释了我的问题。 :) –

1

首先,不要将方法添加到内置的本机对象。你在那里做什么,与Object.prototypeArray.prototype是错误的。

这里的原因:

下面是正确的代码,根据您的逻辑:

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"} 
 
]; 
 

 
function countForBreeding(cattle) { 
 
    // store the length of the array so we don't access it every time 
 
    var cattleCount = cattle.length; 
 

 
    // set a default value 
 
    var numToBreed = 0; 
 

 
    // iterate over the list 
 
    for (var i = 0; i < cattleCount; i++) { 
 
    // verify if our current cow/bull/calf had a calf 
 
    if (hadCalf(cattle[i])) { 
 

 
     // increment if true 
 
     numToBreed++; 
 
    } 
 
    } 
 

 
    return numToBreed; 
 
} 
 

 
function hadCalf(cow) { 
 
    // verify if we have a cow and if it did not have a calf 
 
    if (cow.type === 'cow' && cow.hadCalf === null) { 
 
    return true; 
 
    } else { 
 
    return false; 
 
    } 
 
} 
 

 
var cowsForBreeding = countForBreeding(forestCows); 
 
console.log(cowsForBreeding);

与方法的问题是,你是不是调用功能正常:

if(this[i].noCalvesYet===true){ 
    numToBreed++; 
} 

应该

if(this[i].noCalvesYet() ===true){ 
    numToBreed++; 
} 

您需要执行noCalvesYet功能。

一个更简单的例子是使用内置的Array.prototype.filter函数并将其与Array.prototype.reduce结合使用。然后你可以过滤你的牛列表,并缩小到只符合你的标准的牛:牛和小牛,然后简单地返回该列表的长度。

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"} 
 
]; 
 

 
var cowsForBreeding = forestCows.filter(function(cow) { 
 
    return cow.type === 'cow' && cow.hadCalf === null; 
 
}); 
 

 
console.log(cowsForBreeding.length);

干杯!

+1

其实我在一周前就开始了JS,并通过Codeschool来学习它,截至目前我正处于学习Prototype的早期阶段,我仍然试图非常仔细地理解它。干杯! –

+1

很好!祝你好运!你也可以试试这本书:http://eloquentjavascript.net/。这是非常初学者友好的,充满练习,并没有采用学术语言或任何其他可能会更复杂的事情。 –