Reputation: 3
The add method isn't working like intended. When I have 2 time object t (all 0's), and t2 (1 minute). When I add t2 to t, t doesn't change BUT when I call my toString method on t it gives me the expected value. Here is what I mean if that makes no sense: An example of what I mean!
What I think the problem is t's global references aren't being updated after the call to add. I just can't understand why.
I would like to know why it isn't working not just how to fix the problem.
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
}
};
Upvotes: 0
Views: 43
Reputation: 4277
Everything works. Because the function
//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;
}
Retuns nothing, you get a undefined
as a result of addition. Please try inputting t
into console after adding and you will see that t
is new.
Upvotes: 0
Reputation: 12028
When you are returning from your Time function your are returning a new object with copied values for year, month, date etc.
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
}
As opposed to the return statment you should just assign the function add
and toString
to the this
reference.
// 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;
}
You do not need to return a value from a constructor. See https://jsfiddle.net/dwat001/xf1Lfvv5/
Upvotes: 1