I'm pretty new to Javascript and am writing a Chrome extension which is offsetting the "click" function call in a list of emails on an emailing website. My problem is that the functions that I'm offsetting are being passed by reference, which is causing them to eventually all point to a single click event.
My code:
//Offsets the email links
for (var i = emails.length - 1; i >= amountToRemove; i--) {
var a = emails[i];
var b = emails[i - amountToRemove];
a.addEventListener('click', (x) => {b.click(); x.stopPropagation(); return false;})
}
And a little elaboration of the problem: ie. say amountToRemove is 2 and i = 10; the first two "shifts" of click() events go well, but in the 3rd shift, the click() function for the 9th (last) and 7th objects in the array point to the same function, which is not what was intended. I've tried making a "deep copy" of the function using
JSON.parse(JSON.stringify(object))
as well as other methods recommended by the internet to no avail.
My question is, is there a way of making a "deep" copy of the .click() functions so that they don't point to the same object and cause each click event to eventually point to the same single click event?