Why am I unable to return the element_id in the run function? The script works if I set it as let self = this; function(){ self.run() }
Doesn't work:
class clickin {
constructor(element_id) {
this.element = document.getElementById(element_id);
this.element.addEventListener('click', this.run);
}
run() {
alert('Element id: ' + element_id);
}
}
Works:
class clickin {
constructor(element_id) {
this.element = document.getElementById(element_id);
let self = this;
this.element.addEventListener('click', function(){
self.run();
});
}
run() {
alert('Element id: ' + element_id);
}
}