所以,現在,我有一些代碼,看起來像這樣的JavaScript函數返回數字,而不是對象
var citibiz2 = new SLAO_stat(4, 25, 4, 55, SplunktimecitiBiz, "citiBiz2", "citiBiz", '0', 1, subcitiBiz);
var SLAO_stat = function(SLAhour, SLAmin, SLOhour, SLOmin, Splunktime, statusName, mainArray, number, criticality, location) {
this.SLAmin = SLAmin;
this.SLAhour = SLAhour;
this.SLOmin = SLOmin;
this.SLOhour = SLOhour;
//set SLA and SLO time for each function
var SLAtoday = new Date(time.year, time.month, time.day, SLAhour, SLAmin);
var SLOtoday = new Date(time.year, time.month, time.day, SLOhour, SLOmin);
//set SLA and SLO time for the next day
var SLAtom = addDay(SLAtoday, 1);
var SLOtom = addDay(SLOtoday, 1);
//set SLA time for 12 hours before current
var SLAyest = addDay(SLAtoday, -.5);
//set SLA and SLO time for the previous day
var SLOyes = addDay(SLOtoday, -1);
var SLAyes = addDay(SLAtoday, -1);
var SLAout;
var SLOout;
//if the thing comes in between the time it came in last time and the SLA time tommorow, it's in.
if (Splunktime > SLAyest && Splunktime < SLAtom) {
SLAout = SLAtom;
SLOout = SLOtom;
} else {
SLAout = SLAtoday;
SLOout = SLOtoday;
}
//color conditionals
if//(Splunktime > SLAtoday || currentTime > midyes && Splunktime < midyes && Splunktime > SLAyest && currentTime < SLOtoday){
(currentTime > SLAtoday && Splunktime > SLAtoday && Splunktime != ""){
displayColor = 4;
main_gray++; //gray status
graydiv = document.getElementById(location);
document.getElementById(statusName).innerHTML = "<a href='#'><img src='gray.jpg' class = 'select' class = 'image-cropper'></a>";
if(criticality == 1) $('#gray_container' + number).prepend($(graydiv));
else $('#gray_container' + number).append($(graydiv));
}else if (currentTime < SLOout) {
//green;
displayColor = 1;
main_green++;
document.getElementById(statusName).innerHTML = "<a href='#'><img src='green.png' class = 'select' class = 'image-cropper'></a>";
} else if (currentTime > SLAout) {
//red
displayColor = 2;
main_red++; //for main status
document.getElementById(statusName).innerHTML = "<a href='#'><img src='red.png' class = 'select' class = 'image-cropper'></a>";
} else if (currentTime<SLAout && currentTime>SLOout) {
//yellow
displayColor = 3;
main_yellow++; //for main status
document.getElementById(statusName).innerHTML = "<a href='#'><img src='yellow.png' class = 'select' class = 'image-cropper'></a>";
} else {
//red
displayColor = 2;
main_red++; //for main status
document.getElementById(statusName).innerHTML = "<a href='#'><img src='red.png' class = 'select' class = 'image-cropper'></a>";
}
if(mainArray == "citiBiz"){
if(displayColor == 4) CBrep = 4;
else if(displayColor == 2 && CBrep < 4)CBrep = 2;
else if(displayColor == 3 && CBrep < 2)CBrep = 3;
else if(displayColor == 1 && CBrep < 2)CBrep = 1;
}
else if(mainArray == "creditETL"){
if(displayColor == 4) CETLrep = 4;
else if(displayColor == 2 && CETLrep < 4) CETLrep = 2;
else if(displayColor == 3 && CETLrep < 2) CETLrep = 3;
else if(displayColor == 1 && CETLrep < 2)CETLrep = 1;
}
else alert("There is no array called " + mainArray);
alert("dc " + displayColor);
return displayColor; //returns [object Object] instead of a number
};
我的代碼做了一系列的事情輸出數量。我想返回數字而不是對象。有沒有辦法做到這一點?
再一次,這是一個真正簡化了我的代碼中正在發生的事情。我真的只是問是否可以做到。謝謝!
你分配一個對象'號'你的代碼中的某處你還沒有發佈。從'newfunc'返回一個數字,並且你將有一個數字而不是一個對象。 –
好的。你有代碼應該返回一個數字。它返回一個對象。你不知道爲什麼。我們應該如何知道發生了什麼?我可以回答「你應該在函數的開頭輸入'return 0;''?沒有你的代碼,我們無法做出正確的答案。 –
我的代碼在這個函數中長100行,但如果你真的覺得它會幫助你,我會發布它。 – CatCodinator