"If you can’t answer this question, you’re a junior developer. I don’t care how long you’ve been coding."
https://medium.com/javascript-scene/master-the-javascript-interview-what-is-a-closure-b2f0d2152b36
Ever since reading this dramatic (and, in retrospect, idiotic) statement, I have been kind of obsessed with figuring out the actual use for closures. I will grant the "mimic private field/property" and the module pattern. Past that though, any intentional use of closures seem limited to "if I loop through a set of dom elements and assign an event, the event will be assigned incorrectly", e.g.
var buttons = document.getElementsByTagName("button");
for (var i = 0; i < buttons.length; i++) {
buttons[i].addEventListener("click", function() {
console.log("My value: " + i);
});
}
// Each button will state "My value is 3" (Instead of 1,2,3)
So, after reading chapters in three books and a ton of articles and stackoverflow posts, I ask you HN - have you ever used a closure intentionally? If so, please share.