Javascript函数返回数字而不是对象

时间:2015-06-29 15:12:16

标签: 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 个答案:

答案 0 :(得分: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