[英]How do I use the result of one function in another?
抱歉,這就是全部...花點時間讓我習慣這個網站。 我不知道你們會這么快。 就像我說的第一個函數運行良好一樣,所以我不明白為什么我不能在getDonutsPD函數中使用this.DonutsPH。
function donutStore(location, minCustPh, maxCustPh, averageDpC, hoursOpen) {
this.location = location;
this.minCustPh = minCustPh;
this.maxCustPh = maxCustPh;
this.averageDpC = averageDpC;
this.hoursOpen = hoursOpen;
this.donutsPerHourValue = 0;
this.getDonutsPH = function() {
console.log(this.donutsPerHourValue = (this.location + " needs " + (Math.floor(Math.random() * (this.maxCustPh - this.minCustPh) + this.minCustPh)) * this.averageDpC) + " donuts per hour.");
};
this.getDonutsPD = function() {
console.log(this.getDonutsPH * this.hoursOpen);
};
var Downtown = new donutStore("Downtown", 8, 43, 4.5, 24);
var CapitolHill = new donutStore("CapitolHill", 4, 37, 2, 12);
var SLU = new donutStore("SouthLakeUnion", 9, 23, 6.33, 24);
var wWood = new donutStore("WestWood", 2, 28, 1.25, 12);
var Ballard = new donutStore("Ballard", 8, 58, 3.75, 16);
var dNutStores = [Downtown, CapitolHill, SLU, wWood, Ballard];
for(i=0; i < dNutStores.length; i ++)
{
dNutStores[i].getDonutsPH();
dNutStores[i].getDonutsPD();
}
this.getDonutsPH
是一個變量。 在javascript函數中也可以是變量。
為了運行任何函數,您需要在函數名稱后附加()
,例如
this.getDonutsPH()
第二件事,做一個console.log('blah')
不會返回任何值,您需要告訴函數返回一個值
this.getDonutsPH = function() {
return this.location + " needs " + (Math.floor(Math.random() * (this.maxCustPh - this.minCustPh) + this.minCustPh)) * this.averageDpC) + " donuts per hour.";
}
更新:JoshBeam評論:-
我也想指出,this.getDonutsPH * this.hoursOpen將返回NaN,因為this.getDonutsPH將返回字符串。 將字符串乘以數字將始終返回NaN。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.