Skip to content

Instantly share code, notes, and snippets.

@jbmilgrom jbmilgrom/buidCallbacks.js
Last active Sep 23, 2019

Embed
What would you like to do?
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
You can’t perform that action at this time.