Skip to content

Instantly share code, notes, and snippets.

@jbmilgrom
Last active September 23, 2019 13:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbmilgrom/204234a1f7faf5be38d2e30ddafb44bc to your computer and use it in GitHub Desktop.
Save jbmilgrom/204234a1f7faf5be38d2e30ddafb44bc to your computer and use it in GitHub Desktop.
Problems with "internal" mutability
const buildCallbacks = items => {
const callbacks = [];
let i;
for (i = 0; i < items.length; i++) {
callbacks.push(() => items[i]);
}
return callbacks;
}
const callbacks = buildCallbacks(["hello", "cruel", "world"]);
callbacks.length; // => 3
callbacks[0](); // => undefined
callbacks[1](); // => undefined
callbacks[2](); // => undefined
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment