In the code below, the pushElement method works just fine when dealing with the "words" variable, but as soon as I run the popElement method, it fails on the "this.words.length" piece with the following error: "Uncaught TypeError: Cannot read property 'length' of undefined".
Any ideas?
function AnimationStack() {
this.words = [];
}
AnimationStack.prototype.pushElement = function(element) {
this.words.push(element);
}
AnimationStack.prototype.popElement = function() {
if (this.words.length>0) {
var element = this.words.shift();
return element;
} else {
return null;
}
}
var AS = new AnimationStack();
var element = $("<div></div>");
AS.pushElement(element); // works perfect
AS.pushElement(element); // works perfect
AS.pushElement(element); // works perfect
var pop = AS.popElement(); // always fails
EDIT: The code above is perfect. It was in my actual implementation of how I was using the code above. I'm using setInterval to call popElement() which changes the scope of "this". Read the full answer here:
http://forrst.com/posts/Javascript_Array_Member_Variable_is_Undefined_wi-g6V
{ }) before you start setting its properties.