Okay, so I did search a bit before posting… no luck (or maybe I’m just stupid).
I have this array I call “myArray” and I push objects onto it to populate some variables:
myArray.push({
time : (y.moveTime - y.startTime),
pos : y.move,
last : myArray[y.recents.length-1].time
});
My issue is why does firebug complain about the “last” variable: “Uncaught TypeError: Cannot read property ‘time’ of undefined“. If I do
last : myArray[y.recents.length-1]
everything is fine.
An observation I don’t understand:
The array is empty when I have the “.time” reference, but if I remove it, the array is full.
What am I missing here? I don’t get it 🙁
Thanks for any pointers.
The error means that the evaluated value of
is not an object that has a
timeproperty. This likely occurs when you perform the firstpushbecause the array does not yet have any elements.If you want to hide the error and just assign the
lastproperty toundefinedin this case, you can just add a fallback value: