2017-04-22 84 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/。這是非常初學者友好的,充滿練習,並沒有採用學術語言或任何其他可能會更復雜的事情。 –