Skip to content

Instantly share code, notes, and snippets.

@kitsune7
Created July 17, 2020 02:47
Show Gist options
  • Save kitsune7/90569d5354b49182a0b3eaf7cc91f521 to your computer and use it in GitHub Desktop.
Save kitsune7/90569d5354b49182a0b3eaf7cc91f521 to your computer and use it in GitHub Desktop.
// Playing around with some different ways of doing for loops
// `forEachIterableProtocol` was the main toy I got to play with
function forEachIterableProtocol (array, action) {
for (let i = array[Symbol.iterator](), obj = i.next(); !obj.done; obj = i.next()) {
action(obj.value)
}
}
function forEachNormal (array, action) {
for (let i = 0; i < array.length; i++) {
action(array[i])
}
}
function forEachIn (array, action) {
for (const item in array) {
action(item)
}
}
function forEachOf (array, action) {
for (const item of array) {
action(item)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment