var calendar = {
activeDates: null,
dateClicked: function () {
this.activeDates = document.querySelectorAll(
'[data-calendar-status="active"]'
)
for (var i = 0; i < this.activeDates.length; i++) {
this.activeDates[i].addEventListener('click', this.eventListener)
}
},
eventListener: function () {
this.removeSelectedClass()
this.classList.add('vcal-date--selected')
},
removeSelectedClass: function () {
// details omitted
},
dateChecked: function () {
//will call removeEventListener here
}
}
I want to call the function removeSelectedClass()
in the event listener callback eventListener
, and I know for sure this.removeSelectedClass()
is wrong as this this
refers to the context object this.activeDates[i]
instead of calendar
. A simple solution involves creating an anonymous function for the event listener callback but it is not possible in this case because I want to reuse eventListener
in dateChecked
. What should I do?