Доброго времени суток
Сейчас я пишу класс и в нем есть метод, который создает слайдер. Внутри данного метода я создал два обработчика событий, но внутри обработчика событий не видны объекты с this.
Сам класс
class sliders{
constructor(leftButton, rightButton, classImg){
this.leftButton = leftButton;
this.rightButton = rightButton;
this.classImg = classImg;
}
createSlider(){
this.sliderButtonLeft = document.getElementsByClassName(this.leftButton)[0];
this.sliderButtonRight = document.getElementsByClassName(this.rightButton)[0];
this.sliderArrayImg = [];
for(this.counter = 1; this.counter <= document.getElementsByClassName(this.classImg).length; this.counter++){
this.sliderArrayImg[this.counter-1] = document.getElementsByClassName(this.classImg)[this.counter-1];
}
this.counter = 0;
this.sliderButtonLeft.onclick = function(){
this.sliderArrayImg[this.counter].style.display = "none";
this.counter--;
this.sliderArrayImg[this.counter].style.display = "block";
}
this.sliderButtonRight.onclick = function(){
this.sliderArrayImg[this.counter].style.display = "none";
this.counter++;
this.sliderArrayImg[this.counter].style.display = "block";
}
}
};
Как сделать так, чтобы обработчик событий смог работать с объктами внутри класса?
this.sliderButtonLeft.onclick = () => {,this.sliderButtonRight.onclick = () => {. – Oct 01 '17 at 15:07