Gunnar Bittersmann: Frage zum Wiki-Artikel „JavaScript“

Beitrag lesen

problematische Seite

@@Rolf B

Grundsätzlich ist der Einsatz von globalen Variablen zu vermeiden.

Ist er?

function updateClickcount(delta) {
   const counterElement = document.getElementById("click_1");

Ich würde sagen: Grundsätzlich sind unnötig wiederholte Suchen von Elementen im DOM zu vermeiden. Das Element muss nur einmal gesucht werden – also außerhalb der Eventhandlerfunktion:

const counterElement = document.getElementById("click_1");

function updateClickcount(delta) {   

Wenn dir die globale Variable nicht schmeckt, kannste das ganze ja noch in eine(n?) IIFE tun.

😷 LLAP

--
“When I was 5 years old, my mother always told me that happiness was the key to life. When I went to school, they asked me what I wanted to be when I grew up. I wrote down ‘happy.’ They told me I didn’t understand the assignment, and I told them they didn’t understand life.” —John Lennon