In my js file I have a class slideshow and an array slideshows which I fill with instances of the slideshow class. Later in my code I use a for loop, where I iterate over each element in slideshows and try to get a specific value from it. However, when trying to run my code I get the following error message:
Uncaught TypeError: slideshows[i].images[slideshows[i].shownImage] is undefined
My code looks like this:
let slideshows = [];
class slideshow{
constructor(images){
this._images = images;
this._shownImage = 0;
}
get images() {return this._images;}
get shownImage() {return this._shownImage;}
set images(images) {this._images = images;}
set shownImage(shownImage) {this._shownImage = shownImage;}
}
// This function is called when the page has loaded in
function setupSlideshow(){
// Create Array of all slideshows
slideshowDivs = document.querySelectorAll("div.slideshow");
for(i = 0; i < slideshowDivs.length; i++){
slideshows.push(new slideshow(slideshowDivs[i].querySelectorAll("slideshow-image")));
}
// Show Active Image
for(i = 0; i < slideshows.length; i++){
slideshows[i].images[slideshows[i].shownImage].style.display = 'block';
// The line above this one triggers the error
}
}
I believe the syntax isn’t the problem, although I don’t have a lot of experience with js classes. The only thing I can think of that could be the problem is that js doesn’t know that slideshows[i] is of class slideshow and thus doesn’t look at the functions I declared in my class.