I'm writing a javascript for my webpage, and was wondering what is the difference between document.addEventListener() and this.addEventListener()? Both work for me, but is there any performance impact between the both options?
e.g.
Option 1:
class SomeThing {
constructor() {
this.my_button = document.getElementById("id-1")
this.events();
}
events() {
this.my_button.addEventListener("click", () => {
console.log("Button clicked");
});
}
}
export default SomeThing;
Option 2:
class SomeThing {
constructor() {
this.events();
}
events() {
document.getElementById("id-1").addEventListener("click", () => {
console.log("Button clicked");
});
}
}
export default SomeThing ; Which option is better and correct, and why? Thanks