I am trying to create a list of objects that are listed in an array. newConstant is a function that creates the objects and them pushes them to the array. However, when the while loop runs through the array and throws up alerts containing one of the properties of each array, it spits out the value for the last object for each object in the array. In this situation it alerts "3" each time, but it should alert "1", then "3", as those are the value of the property x for the two objects in the array "a". The code is below. How can I fix this?
var i = 0;
var a = [];
var newConstant = function (x, y) {
this.x = x;
this.y = y;
a.push(this);
};
var one = newConstant(1, 2);
var two = newConstant(3, 4);
while (i < a.length) {
alert(a[i].x);
i++;
}
window.