Skip to content

Instantly share code, notes, and snippets.

@patridge
Created August 20, 2020 19:08
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 patridge/fbdb0114c1cf10c8880fa8abd3ee15fe to your computer and use it in GitHub Desktop.
Save patridge/fbdb0114c1cf10c8880fa8abd3ee15fe to your computer and use it in GitHub Desktop.
// e.g., inject something whenever iterating a list
const arr = [ 1, 2, 3 ];
arr[Symbol.iterator] = function () {
let i = 0;
let arr = this;
return {
next: function () {
if (i >= arr.length) {
return { done: true };
} else {
const value = arr[i] + "🎵";
i++;
return { value, done: false };
}
}
};
};
for (const x of arr) { console.log(x); } // => 1🎵\n2🎵\n3🎵
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment