Skip to content

Instantly share code, notes, and snippets.

@AlexanderZeilmann
Created May 18, 2016 10:27
Show Gist options
  • Save AlexanderZeilmann/7fa9b24cb2941dc414bbd671b66bd0ad to your computer and use it in GitHub Desktop.
Save AlexanderZeilmann/7fa9b24cb2941dc414bbd671b66bd0ad to your computer and use it in GitHub Desktop.
// the array of functions
const fns = [
function () {
console.log(1)
},
function () {
console.log(2)
},
function () {
console.log(3)
}
]
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Method_definitions
const call = {key(func) {func()}}.key
fns.forEach(call) // change this line if you like
// Expected console output should be
// 1
// 2
// 3
@timoxley
Copy link

ahhh, yeah I didn't disallow method definitions explicitly, but I wouldn't consider this a valid solution as it's basically creating an anonymous function without creating an anonymous function. The intended point of this quiz is to get people thinking about how to invoke a function without creating a new wrapper function.

@timoxley
Copy link

It does mention "do not create named functions". Updated gist to explicitly disallow method definitions.

@timoxley
Copy link

Though you're the first one to use this trick so you get some points for creativity 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment