Closure classic
How do I make an closure classic?
What is a closure classic? How do you make a closure classic? This script and codes were developed by Nick Williams on 27 August 2022, Saturday.
Closure classic - Script Codes HTML Codes
<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>closure classic</title>
</head>
<body> <p>This is a classic example of where you need a closure. Notice that every button reports the same value (the final value of <code>i</code>). The reason for this is each function shares the same scope, so each callback refers to the exact same variable <code>i</code>.</p>
<button class="without-closure">Click</button>
<button class="without-closure">Click</button>
<button class="without-closure">Click</button>
<button class="without-closure">Click</button>
<button class="without-closure">Click</button>
<p>The problem is sharing of scope, so the solution is of course to create a new scope (via a closure) for each event listener. This sounds complex but the solution is always the same: <em>create your callback/event listener within another function:</em></p>
<button class="with-closure">Click</button>
<button class="with-closure">Click</button>
<button class="with-closure">Click</button>
<button class="with-closure">Click</button>
<button class="with-closure">Click</button>
<p>The reason for this is that JavaScript (confusingly) has function-level scope, so we call another function to create a new scope.</p> <script src="js/index.js"></script>
</body>
</html>
Closure classic - Script Codes JS Codes
var buttons = document.querySelectorAll(".without-closure");
for(var i = 0, length = buttons.length; i < length; i++) { buttons[i].addEventListener("click", function() { alert(i); },false);
}
buttons = document.querySelectorAll(".with-closure");
function createCallback(value) { return function() { alert(value); }
}
for(var i = 0, length = buttons.length; i < length; i++) { buttons[i].addEventListener("click", createCallback(i),false);
}
Developer | Nick Williams |
Username | WickyNilliams |
Uploaded | August 27, 2022 |
Rating | 3 |
Size | 1,842 Kb |
Views | 36,432 |
Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!
Name | Size |
And just like that... | 4,503 Kb |
Particle particulars | 3,688 Kb |
Bokeh | 4,253 Kb |
A Pen by Nick Williams | 1,616 Kb |
React starter template | 1,957 Kb |
Conditional Lightbox for Responsive Design | 2,792 Kb |
Pointed | 2,630 Kb |
Headroom.js demo | 3,982 Kb |
Gridzzz | 3,199 Kb |
JS module pattern demo | 4,111 Kb |
Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!
Name | Username | Size |
Twitch | SarahDunlap | 2,937 Kb |
Search field | Jamesbarnett | 2,100 Kb |
Pure CSS Torch Light | Juliendargelos | 2,727 Kb |
Animated Donut Chart | Jplhomer | 3,808 Kb |
Responsive slide | Thorien | 2,400 Kb |
Mega menu | DimaZubkov | 5,066 Kb |
Savemedia1.1 | EdsonAlcala | 2,148 Kb |
Draggables in pure angular | Rlo206 | 5,167 Kb |
Countdown with Rings | Ewganoel | 2,490 Kb |
Very Simple Slider | Doodlemarks | 2,682 Kb |
Surf anonymously, prevent hackers from acquiring your IP address, send anonymous email, and encrypt your Internet connection. High speed, ultra secure, and easy to use. Instant setup. Hide Your IP Now!