1

I've reviewed previous posts on this problem, but have not been able to get the generally proposed solution to work for me. evtList is a global array of objects of type "Event". Previous posts have indicated that creating a local object ("newEvent") inside my loop, defining it's elements, and then pushing the local object into the global array should work. However, I've found that as soon as I declare a new value for an element in the newEvent object, all the objects in the global array are changed to that set of values even before the push operation. What have I missed?

var i = 0;
var nextTime = new Date();

while (evtList[i].evtTime < dayEnd) {
    i++;
    var newEvent = new Event;
    var milliSecs = (evtList[i-1].evtTime).getTime() + gaussian(arrStations[0].mean, arrStations[0].stdDev);
    nextTime.setTime(milliSecs);
    newEvent = ({value: 1, station: 0, evtTime: nextTime});

    evtList.push(newEvent);
}
4
  • you're adding to the array you're looping over? Commented Dec 29, 2016 at 12:58
  • Why do you assume that the object are the same? They are differnt object, but the evtTime will be the same for all objects because they all reference the same date (nextTime). Beside that, the newEvent element you push is a plain JavaScript object and not an object of the type Event. Commented Dec 29, 2016 at 13:02
  • the next time value would change even before you push since you are updating the same reference which is present of all newEvent objects......so even though you are adding a new object....the constituents of the object have the same values......this is why you see that all objects have the same value Commented Dec 29, 2016 at 13:07
  • I'm building an array of timeline events (objects). Each new event occurs at a normally distributed interval after the previous event. The timeline stops when it reaches or surpasses the dayEnd Date value set by the user., Commented Dec 29, 2016 at 13:08

1 Answer 1

1

In JavaScript var does not define a block level scope but a function level scope, so newEvent is not local to the your for loop, but to the the function body it belongs to, or to the global scope if it is not in a function.

With you push you add different objects to your evtList, but the problem is that the property evtTime of each of your objects reference the same Date object, so with nextTime.setTime(milliSecs) you will change the date for all of your evtTime properties, because it is the same object.

You would need to create a new Date object for each of you objects:

var newEvent;
var milliSecs;
var nextTime;
var i = 0;

while (evtList[i].evtTime < dayEnd) {
  i++;
  // newEvent = new Event;
  nextTime = new Date;
  milliSecs = (evtList[i - 1].evtTime).getTime() + gaussian(arrStations[0].mean, arrStations[0].stdDev);
  nextTime.setTime(milliSecs);
  newEvent = ({
    value: 1,
    station: 0,
    evtTime: nextTime
  });

  evtList.push(newEvent);
}

The newEvent you push to your evtList is not of the type Event, because the Event object that you have created with newEvent = new Event overwritten by a plain JavaScript object here:

newEvent = ({
  value: 1,
  station: 0,
  evtTime: nextTime
});

You either want to set just the properties of the newly created Event object:

newEvent.value = 1;
newEvent.station = 0;
newEvent.evtTime = nextTime;

Or you might pass them to your Event constructor.

Sign up to request clarification or add additional context in comments.

1 Comment

Perfect! That worked! Thank you so much for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.