So i have this equalizer
function here, which gives all elements the same minHeight. The function uses lodash's throttle function and executes everytime the window resizes and on load.
export const equalizer = throttle((elems, e, stopAt = 0) => {
const elements = [...document.querySelectorAll(elems)];
const setMinHeight = (el) => el.style.minHeight = '0px';
if (e.type === 'resize') elements.forEach(setMinHeight);
const minHeight = elements.reduce((acc, cur) => {
if (cur.clientHeight >= acc) return cur.clientHeight;
if (acc >= cur.clientHeight) return acc;
}, 0);
elements.forEach((el) => (el.style.minHeight = `${minHeight}px`));
}, 500);
So the "problem" now is that everytime I call this function I have to pass in the event object
(because of e.type === "resize"
) and I want to know if there is a way to avoid that.
['load', 'resize'].forEach((event) => {
window.addEventListener(event, function (e) {
equalizer('.comment__body p', e);
equalizer('.comment__header .comment__name', e);
});
});
I thought that I don't even need to pass in the event object because of closures. And the way I understand closures is that a function has access to it's outer functions variables.
So is there a way to access the event object
in this equalizer
function without me having to everytime passing it in?
Comments
Post a Comment