QuickAnswer
by

this in JavaScript changes. How to keep it unchanged.

this in JavaScript changes. How to keep it unchanged.

If you don't want this in JavaScript to change, you can follow the rules below.

const abc1 = () => {
    console.log(this);
};
const abc2 = function() {
    console.log(this);
};
function abc3() {
    console.log(this);
}

elm.addEventListener('click',abc1);    //this does not change
elm.addEventListener('click',abc2);    //this is elm
elm.addEventListener('click',abc3);    //this is elm

//this does not change in any of the following
elm.addEventListener('click',() => {abc1();});
elm.addEventListener('click',() => {abc2();});
elm.addEventListener('click',() => {abc3();});
elm.addEventListener('click',function(){abc1();});
elm.addEventListener('click',function(){abc2();});
elm.addEventListener('click',function(){abc3();});
CONTENTS
Web Browser