어떻게 다른 함수에서 하나의 함수를 사용한 결과입니까?
질문:
미안해요. 지금 이게 다예요...나는 이 사이트에 적응하는 데 약간의 시간을 썼다.나는 너희들이 이렇게 빨리 할 줄 몰랐다.내가 말한 첫 번째 함수가 잘 작동하기 때문에 나는 왜 내가 그것을 사용할 수 없는지 모르겠다.getDonutsPD 함수의 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
는 변수다.자바스크립트에서 함수도 변수일 수 있다.모든 함수를 실행하기 위해서는 함수 이름 뒤에
()
를 추가해야 합니다. 예를 들어.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 댓글:-나도 이 점을 지적하고 싶다.이거.hoursoen이 NaN으로 돌아갑니다.getDonutsPH는 문자열을 반환합니다.문자열을 숫자에 곱하면 항상 NaN이 반환됩니다.