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();
}
答案 0 :(得分:3)
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。