Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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?


  • Do not use the function keyword, or arrow functions () =>.
  • Do not invoke the Function constructor.
  • Do not use method definitions.
  • 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.


Please don't post answers here (or in reply to the original tweet)

Do feel free to tweet them at me

// the array of functions
const fns = [
function () {
function () {
function () {
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

Should we be leaving answers here, or would that spoil the fun?


timoxley commented May 12, 2016

@subshad yep, I think that would spoil the fun. You can tweet them at me on twitter.

lewisje commented May 17, 2016

@Lucifier129 came to the same conclusion I did, but I was inspired by its use in es6-shim; also, this old jsPerf comparison is relevant.


timoxley commented May 18, 2016

@unlight had not seen that before. Good explanation there.

Since this solution didn't come up so far, I thought I post it.

Similar technique can be used to transform methods working on this like String#toUpper into a function from normal argument.
Look ma, point-free!

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