Skip to content

Commit

Permalink
Recreate old bug caused by capturing a var in a closure.
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyestein committed Dec 31, 2023
1 parent a42065d commit c654da9
Showing 1 changed file with 4 additions and 10 deletions.
14 changes: 4 additions & 10 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,16 +128,10 @@ function makeCopyButtons() {
var item = dataItemFromBI(bi);
var butt = document.createElement('BUTTON');
butt.className = copy_button_class;
// I'm not sure why this mess is needed, but I was previously getting
// every button copying the *last* value copyString had in the loop.
function onclickgen(copyStr) {
function onclickinner() {
navigator.clipboard.writeText(copyStr);
}
return onclickinner;
}
let copyString = formatItemAsStr(item);
butt.onclick = onclickgen(copyString);
// BUG!
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures#creating_closures_in_loops_a_common_mistake
var copyString = formatItemAsStr(item);
butt.onclick = () => { navigator.clipboard.writeText(copyString); };
butt.innerHTML = 'Copy';
bi.prepend(butt);
}
Expand Down

0 comments on commit c654da9

Please sign in to comment.