sorry this is all of it now...taking me a while to get used to this site. I had no idea you guys would be so fast. Like I was saying the first functions runs fine, so I don't understand why I can't use this.DonutsPH in the getDonutsPD function.
对不起,这就是现在所有的...让我一段时间习惯这个网站。我不知道你们会这么快。就像我说的第一个函数运行正常,所以我不明白为什么我不能在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();
}
1 个解决方案
#1
this.getDonutsPH
is a variable. In javascript functions can also be variables.
this.getDonutsPH是一个变量。在javascript函数中也可以是变量。
In order to run any function you need to append ()
to the function name, e.g.
要运行任何函数,您需要将append()附加到函数名称,例如
this.getDonutsPH()
Second thing, doing a console.log('blah')
does not return any value, you need to tell the function to return a value
第二件事,做一个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.";
}
Update: JoshBeam comment:-
更新:JoshBeam评论: -
I want to point out, too, that this.getDonutsPH * this.hoursOpen will return NaN, because this.getDonutsPH will return a string. Multiplying a string by a number will always return NaN.
我也要指出,this.getDonutsPH * this.hoursOpen将返回NaN,因为this.getDonutsPH将返回一个字符串。将字符串乘以数字将始终返回NaN。
#1
this.getDonutsPH
is a variable. In javascript functions can also be variables.
this.getDonutsPH是一个变量。在javascript函数中也可以是变量。
In order to run any function you need to append ()
to the function name, e.g.
要运行任何函数,您需要将append()附加到函数名称,例如
this.getDonutsPH()
Second thing, doing a console.log('blah')
does not return any value, you need to tell the function to return a value
第二件事,做一个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.";
}
Update: JoshBeam comment:-
更新:JoshBeam评论: -
I want to point out, too, that this.getDonutsPH * this.hoursOpen will return NaN, because this.getDonutsPH will return a string. Multiplying a string by a number will always return NaN.
我也要指出,this.getDonutsPH * this.hoursOpen将返回NaN,因为this.getDonutsPH将返回一个字符串。将字符串乘以数字将始终返回NaN。