I keep running into this problem. And right now it has to do with the Date object
var now = new Date();
var later = new Date();
later.setHours( later.getHours() + 8 );
<Wait for somthing>
now = later;
later.setHours( later.getHours() + 8 );
alert(now == later); //returns True
I thought this could be solved using callbacks:
var adjustTime = function(callback){
now = later;
callback();
}
adjustTime(function(){
later.setHours( later.getHours() + 8 );
});
alert(now == later); //returns True
What do I not understand? How can I update these variables correctly?
EDIT:
Ok, I should explained myself a bit better. What I want to do is to update the now variable to be the value of later. And after that I want to increase later with 8 hours.
Equality comparisons between
Dateinstances just tests for object reference equality. And in any case, once you’ve done this:then the variables “now” and “later” reference the same object. Updates to “later” are therefore updates to “now”.
The “setter” methods on the
Dateprototype all modify the object directly. That is,Dateinstances are not immutable.edit — I’m going to guess that what you really want is to keep “now” and “later” separate. In that case, something like this is probably what you want:
After that, “now” and “later” won’t be equal because you’ll have set it to a new
Dateinstance.