Skip to content

Instantly share code, notes, and snippets.

@dimorphic
Forked from timoxley/Readme.md
Last active May 18, 2016 09:08
Show Gist options
  • Select an option

  • Save dimorphic/36eb4d272182075ceb8cfeafffd1b0f4 to your computer and use it in GitHub Desktop.

Select an option

Save dimorphic/36eb4d272182075ceb8cfeafffd1b0f4 to your computer and use it in GitHub Desktop.
JS Pop Quiz: How well do you know your functions?

JS Pop Quiz: How well do you know your functions?

Given an Array of Functions fns, what argument(s) can you pass to fns.forEach such that each function in fns will execute, in order, without creating any anonymous (or named) functions or invoking the Function constructor?

Conditions

  • Do not use the function keyword, or arrow functions () => .
  • Do not invoke the Function constructor.
  • Function#bind & friends on the Function.prototype are ok.
  • Answer should be a single line.
  • Try for clarity of concept rather than fewest characters; Reduce prototypical lookup to an absolute minimum.
  • Create no variables nor use any flow control structures i.e. no loops.
  • No Asynchrony (?!)
  • No unnecessary semicolons (just because)

Once you have a solution that works, make sure you can explain exactly why it works, and why your previous solutions didn't work.

Hint

// the array of functions
const fns = [
function () {
console.log(1)
},
function () {
console.log(2)
},
function () {
console.log(3)
}
]
const call = // ??? your implementation here. Do not use the `function` keyword or arrow functions.
fns.forEach(call) // change this line if you like
// Expected console output should be
// 1
// 2
// 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment