0

I want to trigger an if statement when the intersectionObserver API toggles a class in the DOM Element. I am just not sure what exactly is the boolean value returned by this expression: - entry.target.classlist.toggle.valueof; or entry.target.classlist.toggle.valueof();

let intersect = new IntersectionObserver ((entries) => {
    entries.forEach((entry) => {
        if (entry.target.classList.toggle("fix", entry.isIntersecting).valueOf) {
            fixHeader();
        }
        else unFixHeader();
    });
}, {
    rootMargin: "-1%",
    threshold: 1
});

  • If it is `.valueOf()`, then it makes little sense. If it is `.valueOf` then it also makes little sense and will cause the if-branch to be always entered (because `.valueOf` is a _function_, which is always a truthy value). Where did you get this code from? – Ivar May 19 '22 at 10:34
  • my code. vs code showed valueof as an option so i selected it because seemed like what i needed. – SuperSaiyan99000 May 19 '22 at 10:46
  • [`.toggle()`](https://developer.mozilla.org/en-US/docs/Web/API/DOMTokenList/toggle) already returns a boolean value. Calling `.valueOf()` on a boolean just returns the boolean value itself so it is completely redundant. Just stick with `entry.target.classList.toggle("fix", entry.isIntersecting)`. – Ivar May 19 '22 at 10:56
  • That made me wonder in what cases will value of will be actually usseful? – SuperSaiyan99000 May 19 '22 at 13:00
  • [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/valueOf) is your friend. :-) – Ivar May 19 '22 at 13:03

0 Answers0