This is a very noobie question but...
I don't understand how this code is working. It has something called the matcher
and I did not even have to declare this "variable" or whatever it is. Also, what is addListener
? I thought it was supposed to be addEventListener
.
The code works, but I don't know how. I do understand how the function OnUpdate
is working, but I don't understand the part below that.
"use-strict";
lightSchemeIcon = document.querySelector("link#light-scheme-icon");
darkSchemeIcon = document.querySelector("link#dark-scheme-icon");
function OnUpdate() {
if (matcher.matches) {
darkSchemeIcon.remove();
document.head.append(lightSchemeIcon);
} else {
document.head.append(darkSchemeIcon);
lightSchemeIcon.remove();
}
}
matcher = window.matchMedia("(prefers-color-scheme: dark)");
matcher.addListener(OnUpdate);
OnUpdate();
I tried searching them up, but the only answers I get are either match()
and addEventListener()
. But, what I want to know about are matcher
and how did it work without any declaration, and addListener
.