2015-06-29 73 views
-3

所以,現在,我有一些代碼,看起來像這樣的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 
    }; 

我的代碼做了一系列的事情輸出數量。我想返回數字而不是對象。有沒有辦法做到這一點?

再一次,這是一個真正簡化了我的代碼中正在發生的事情。我真的只是問是否可以做到。謝謝!

+1

你分配一個對象'號'你的代碼中的某處你還沒有發佈。從'newfunc'返回一個數字,並且你將有一個數字而不是一個對象。 –

+1

好的。你有代碼應該返回一個數字。它返回一個對象。你不知道爲什麼。我們應該如何知道發生了什麼?我可以回答「你應該在函數的開頭輸入'return 0;''?沒有你的代碼,我們無法做出正確的答案。 –

+0

我的代碼在這個函數中長100行,但如果你真的覺得它會幫助你,我會發布它。 – CatCodinator

回答

0

問題在於你用new operator調用該函數。這是構建一個新的對象。根據您所處的大環境,您有兩種選擇。

選項1

不要創建一個新的對象,並調用函數作爲普通函數:

var citibiz2 = SLAO_stat(4, 25...) 

選項2

設置displayColor作爲您正在創建和使用它的對象的屬性。因此,而不是:

return displayColor; 

你可以這樣做:

this.displayColor = displayColor; 

,然後訪問它多數民衆贊成由new創建的對象上:

var citibiz2 = new SLAO_stat(4, 2...) 
alert(citibiz2.displayColor); //should be a number 
+0

謝謝你的迴應!我想爲如此新的東西而道歉,因爲它帶來了愚蠢的錯誤。 我沒有說這是一個使用tomcat的網站。你告訴我使用的第一種方法實際上使我的網站崩潰。第二個返回undefined。 – CatCodinator

+0

第一種方法明顯改變了行爲 - 我可以理解它使其他東西不起作用。第二個不應該顯示'undefined' - 你可以仔細檢查以確保代碼中的拼寫正確嗎?你應該有[相當於這個](http://jsfiddle.net/dhz3eepL/)(或多或少)。 –

+0

它的工作!謝謝! – CatCodinator

相關問題