为什么私有var的引用没有被更新?

时间:2015-06-08 22:12:50

标签: javascript class scope closures

add方法无法正常工作。当我有2个时间对象t(全0)和t2(1分钟)时。当我将t2添加到t时,当我调用我的toString方法时,t不会改变但是它给出了我期望的值。如果没有意义,这就是我的意思:An example of what I mean

我认为问题是在调用add之后没有更新全局引用。我只是不明白为什么。

我想知道为什么它不起作用而不仅仅是如何解决问题。

Time = function(pyear, pmonth, pdate, phour, pminute, psecond)
{
    var that = this;
    that.year = 0;
    that.month = 0;
    that.date = 0;
    that.hour = 0;
    that.minute = 0;
    that.second= 0;

    //constructor
    if(pyear !== undefined)
    {
        that.year = pyear;
        that.month = pmonth;
        that.date = pdate;
        that.hour = phour;
        that.minute = pminute;
        that.second = psecond;
    }

    //adds toAdd time object to time
    function add(toAdd)
    {
        that.year += toAdd.year;
        that.month += toAdd.month;
        that.date += toAdd.date;
        that.hour += toAdd.hour;
        that.minute += toAdd.minute;
        that.second += toAdd.second;
    }



    //returns time as a String
    function toString()
    {
        var toReturn = "";
        if(that.year < 1000)
        {
            if(that.year < 100)
            {
                if(that.year < 10)
                {
                    toReturn += "0";
                }
                toReturn += "0";
            }
            toReturn += "0";
        }

        toReturn += that.year + "-";

        if(that.month < 10)
        {
            toReturn += "0";
        }

        toReturn += that.month +"-";

        if(that.date < 10)
        {
            toReturn += "0";
        }

        toReturn += that.date +"T";

        if(that.minute < 10)
        {
            toReturn += "0";
        }

        toReturn += that.minute +":";

        if(that.second < 10)
        {
            toReturn += "0";
        }

        toReturn += that.second;
        //year +"-"+ month +"-"+ date +"T"+ hour +":"+ minute +":"+ second
        return toReturn;
    }

    return{
        year: that.year,
        month: that.month,
        date: that.date,
        hour: that.hour,
        minute: that.minute,
        second: that.second,
        add: add,
        toString: toString
    }
};

2 个答案:

答案 0 :(得分:1)

当您从时间函数返回时,您将返回一个具有年,月,日等复制值的新对象。

return{ // Create a new Object
    year: that.year,    // copy the value from that.year to year
    month: that.month,
    date: that.date,
    hour: that.hour,
    minute: that.minute,
    second: that.second,
    add: add,
    toString: toString
}

与返回语句相反,您应该将函数addtoString分配给this引用。

 // return{
 //    year: that.year,
 //    month: that.month,
 //    date: that.date,
 //    hour: that.hour,
 //    minute: that.minute,
 //    second: that.second,
 //    add: add,
 //    toString: toString
 //}
 this.toString = toString;
 this.add = add;
}

您不需要从构造函数返回值。见https://jsfiddle.net/dwat001/xf1Lfvv5/

答案 1 :(得分:0)

一切正常。因为功能

//adds toAdd time object to time
function add(toAdd)
{
    that.year += toAdd.year;
    that.month += toAdd.month;
    that.date += toAdd.date;
    that.hour += toAdd.hour;
    that.minute += toAdd.minute;
    that.second += toAdd.second;
}

什么都不回复,因为添加会得到undefined。添加后请尝试在控制台中输入t,您会看到t是新的。