This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var defaultPrint = (item) => console.log(`Hello, ${item}`) | |
var myForEach = (list, printFunction = defaultPrint) => { | |
list.forEach((item) => printFunction(item)) | |
} | |
myForEach([1,2,3]) | |
myForEach(["D","K","C"]) | |
myForEach([{name: "Tomba!"},{name: "Neo Cortex"}], (person) => defaultPrint(person.name)) | |
myForEach([{brand: "Ferrari"}, {brand: "Mercedes"}], (car) => defaultPrint(car.brand)) | |
myForEach([{name: "N64", arch: "MIPS"}, {name: "3DS", arch: "ARM9"}], (console) => defaultPrint(`${item.name} - ${item.arch}`)) | |
// in fact we could even use `Array.prototype.forEach` function and avoid the duplication with `myForEach` | |
// (which does basically what forEach does) | |
// var forEach = Array.prototype.forEach | |
// forEach.call([{name: "Tomba!"},{name: "NeoCortex"}], (person) => defaultPrint(person.name)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment